.Pls file parsing - iphone

Iam creating and radio application in which i have to check the authentication and after successful login i get .pls file in which there is actual url which has to be played so
anybody know how to parse .pls file and get the url inside the .pls file.

In my case I have a WWEMAC.pls file in main bundle. In which more than one url exist and I have to extract only those URLs and add them in array in string format. Following code worked for me.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"WWEMAC" ofType:#"pls"];
NSError *errorReading;
NSArray *linesOfText = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&errorReading] componentsSeparatedByString:#"\n"];
NSLog(#"Reading error %#",errorReading);
NSString *completeDataInStr = [linesOfText componentsJoinedByString:#" "];
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [detector matchesInString:completeDataInStr options:0 range:NSMakeRange(0, [completeDataInStr length])];
for (NSTextCheckingResult *match in matches)
{
NSString *str = [NSString stringWithFormat:#"%#",match.URL];
NSLog(#"URL str = %#",str);
}
return matches;

Related

Reading from text file - Objective C

I am trying to familiarize myself with objective C, and my current goal is to read a list of items in a text file and store them in a NSString array.
Currently this is what I have:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSData* data = [NSData dataWithContentsOfFile:filepath];
NSString* string = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSString* delimiter = #"\n";
listArray = [string componentsSeparatedByString:delimiter];
I am not sure if this matters, but myList.txt is in my Supporting Files.
At the moment, I only have one item in my list. However I am unable to store even that 1 item into my listArray.
I am sure it is something silly that I am missing, I am just new to Objective C.
EDIT:
I apologize for not mentioning this earlier. I AM NOT receiving any sort of error. My array is just null.
I'm going to suggest a little simplification which might solve your problem since I can't say what your problem is. From the information I'm not sure if you are getting the proper file contents when reading it in or not.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
if (error)
NSLog(#"Error reading file: %#", error.localizedDescription);
// maybe for debugging...
NSLog(#"contents: %#", fileContents);
NSArray *listArray = [fileContents componentsSeparatedByString:#"\n"];
NSLog(#"items = %d", [listArray count]);
If the content of the file is just like:
[{"Title":"20","Cost":"20","Desc":""},{"Title":"10","Cost":"10.00","Desc":""},{"Title":"5","Cost":"5.00","Desc":""}]
try this
-(id)readFromDocumentDBFolderPath:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSData* data = [NSData dataWithContentsOfFile:appFile];
id resultData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (error == NULL)
{
return resultData;
}
}
return NULL;
}

Parsing HTML <Tag> into ios

i am Parsing HTML Tag into iOS using Hpple. i am able to parse the data where the HTML Tag is
<div id="NewsPageSubTitle">
<p><**span** hi how are you>
Using ios code:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p/span ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but in few case i don't have span, imean the string in html is accessed by tag "p" directly like:
<div id="NewsPageSubTitle">
<p>< hi how are you>
Here I am using ios code as:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but here i am getting a blank data in response.
can any one let me know how to solve the problem?
Since sometimes the para tag has span and sometimes it does not, I would suggest trying to handle that by looping over the children
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
TFHpple * tutorialsParser = [[TFHpple alloc] initWithHTMLData:data];
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageSubTitle']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement * element in tutorialsNodes) {
NSLog(#"%#", element);
NSLog(#"%#", [element tagName]);
NSLog(#"%#", [element attributes]);
NSLog(#"%#", [element children]);
for (TFHppleElement *childElement in [element children]) {
NSLog(#"%#", childElement);
}
}
Check with this: https://github.com/mwaterfall/MWFeedParser
This will provide the HTML Parser for iphone sdk.
More help on:
this blog and here.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"html" inDirectory:#"New Folder 2"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
TFHpple * Parser = [[TFHpple alloc] initWithHTMLData:data];
NSString *query = #"//p";
NSArray *nodes = [Parser searchWithXPathQuery:query];
for (TFHppleElement *item in nodes)
{
NSLog(#"Title : %#", item.content);
NSLog(#"URL : %#", [item.attributes valueForKey:#"href"]);
}

Cocoa error 256, When using initWithContentsOfURL:

i getting data from my site:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
contents have Cocoa error 256. where i wrong?
The issue is in Hebrew characters, you should html-escape them, also try request with English characters instead, to see if it works
- (void)yourMethod
{
NSString *name = #"שימרגוליס";
name = AFURLEncodedStringFromStringWithEncoding(name, NSUTF8StringEncoding);
NSString *website = [NSString stringWithFormat:#"http://www.ba-cafe.com/fbconnect.php?email=%#&name=%#&pass=SwHyK17!",#"email#mail.com",name];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
}
Where AFURLEncodedStringFromStringWithEncodingis a function from AFNetworking framework
Check the Console log for NSLog(#"%#", website);
You will see something like this:
http://www.mysite.com/fbconnect.php?email=thetrimmedname&name=emailaddress&pass=***
So do this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", [jsonObject objectForKey:#"email"], nameTrimmmed ];
instead of this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
This is because of the dot in the email address.
Look right here:
Error while trying to access Google translate with NSURL

How to retrieve the Xml file from Cachedirectory?

Im new to Iphone development, I have locally saved one xml file and showing the details by using NSXmlParser(Which is stored in Resource folder). But, now i want to download new xml from url(from client side) and need to store that new xml file in Cache directory and also delete the xml file from resource folder. but i did not parse the new xml file from cache directory, How can i parse the new xml file from cache directory? Any one know this, please help me
I've actually just recently written this functionality for one of my projects. I'll share the code i used to do to save and write the XML file to my cache.
- (NSString *)XMLCacheFilePath
{
NSString *directory = #"/Library/Caches/XML/";
NSString *fileName = [#"file" stringByAppendingPathExtension:#"xml"];
// Setup directory path
NSString *homeDirectoryPath = NSHomeDirectory();
NSString *unexpandedDirectoryPath = [homeDirectoryPath stringByAppendingString:directory];
NSString *directoryPath = [NSString pathWithComponents:[NSArray arrayWithObjects:
[NSString stringWithString:[unexpandedDirectoryPath stringByExpandingTildeInPath]],
nil]];
// Setup file path
NSString *unexpandedFilePath = [directoryPath stringByAppendingPathComponent:fileName];
NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:
[NSString stringWithString:[unexpandedFilePath stringByExpandingTildeInPath]],
nil]];
if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:nil]) {
NSError *error = [[NSError alloc] init];
[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
return filePath;
}
- (void)saveXMLStringToDisk:(NSString *)xmlString
{
NSError *error;
[xmlString writeToFile:self.XMLCacheFilePath
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
}
- (NSString *)XMLStringFromDisk
{
NSString *XMLString = [NSString stringWithContentsOfFile:self.XMLCacheFilePath
encoding:NSUTF8StringEncoding
error:nil];
return XMLString;
}

How to save and retrieve text which is in UITextView using plist

Hi How to save and retrieve text which is in UITextView? If anybody knows please help me. Give me some sample code using plist.
Thank you.
Have a look at stringWithContentsOfFile:encoding:error: and writeToFile:atomically:encoding:error:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myfile.plist"];
NSError *error = nil;
// read from plist:
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (!text) {
NSLog(#"Error reading - %#", error);
}
textView.text = text;
// save to plist:
NSString *text = textView.text
if (![text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(#"Error saving - %#", error);
}