Download and parse textfile in Objective-C - objective-c

So far I've kept app data on remote servers in the plist format. Downloading and parsing this format is relatively easy:
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com/data.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfURL:url];
How can I download a text file with an arbitrary format using Objective-C?
An example of arbitrary format is a file that's two sections of CSV data separated by a few blank lines. Of course I'll know the format ahead of time.

Use -[NSString initWithContentsOfURL:encoding:error:, like so
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com/data.plist"];
NSError *error = nil;
NSString *text = [[NSString alloc] initWithContentsOfURL: url
encoding: NSUTF8StringEncoding
error: &error];
This will get you the file's contents which you can treat however you need to.

Related

Writting the contents of an array to a text file

I'm trying to take the contents of an array and save that data into a text file and then email that file. The text file shows up on the email screen, but when I actually receive the email, nothing is attached. I'm not getting any errors, but I'm also not sure that my data is actually being written into my text file.
// create text file
NSString *path = [[NSBundle mainBundle] pathForResource:#"EmailSignUpData"
ofType:#"txt"];
NSError *error = nil;
NSString *data = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
// write the contents of the array into the text file to send
[personArray writeToFile:data atomically:YES];
// attach the text file
NSData *myData = [NSData dataWithContentsOfFile: path];
[mailer addAttachmentData:myData mimeType:#"text/plain"
fileName:#"EmailSignUpData.txt"];
Thanks for the help in advance!!

Getting webpage from data object

I'm fairly new to objective c but I'm using this code
NSURL *url = [NSURL URLWithString:#"http://xxx.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSURLResponse *resp = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:nil];
NSLog(#"received %???", [data XYZ]);
to try and grab the raw content of a webpage. What would I have to insert instead of XYZ (and obviously ???) so that I could just print the website data. I know that the code pulls some data down because I can use XYZ = length to get the size of the incoming data. If anyone could help, that would be great!
(If anyone could additionally explain how I could have found this in the apple documentation, that would also be really useful)
I assume you mean converting the NSData to a NSString so you can print it?
You should use
[NSString stringWithData:data encoding:NSUTF8StringEncoding]
to create a string from the data. Note that the encoding is not UTF8 for all websites so it might not produce the correct output always.
You can easily find these methods by browsing the online documentation: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Separate a .txt file into separate strings

I already know how to upload a file and place its contents into a string with:
NSString *fileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"WellBalanced Recipes" ofType:#"txt"]
encoding:NSUTF8StringEncoding
error:NULL];
However, I would like to parse this text document into separate strings. For example, if I had different recipes in the same document, I would like to somehow separate each recipe.
Use - (NSArray *)componentsSeparatedByString:(NSString *)separator
NSArray *data = [fileContents componentsSeparatedByString:#"\n"];

Convert NSData to a NSURL

I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];

extract data from cocoa http get request

I implemented the following code:
NSURL *url = [ NSURL URLWithString:[ NSString stringWithFormat: #"http://www.google.com/search?q=%#", query ] ];
NSURLRequest *request = [ NSURLRequest requestWithURL: url ];
I want to extract the body from what I receive back from the url above. I attempted:
NSData *data = [request HTTPBody];
The data variable doesn't return any data? Am I going about extracting the data out of the request the right way?
Thanks!
If you're just trying to get a web page, you can use this.
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: #"http://www.google.com"] ];
NSString *test = [NSString stringWithContentsOfURL:url];
If you really want to convert the data from NSData you can use this:
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURLRequest just defines a request — it doesn't do anything by itself. To actually make a request, you need to give the request to an NSURLConnection.
Also, as indicated in the documentation, the HTTPBody is data that's sent with the request, not the response body.
There is an article on www.eigo.co.uk which shows exactly how to do the request and get the response in a string variable but the chunk of code you need is...
NSString * strResult = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
Check out the article here http://www.eigo.co.uk/iPhone-Submitting-HTTP-Requests.aspx