How to download a text file with iPhone SDK?
cocoa-touch, download, iphone
Solution
NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://myurl.com/mypage"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
//HANDLE ERROR HERE
}
Then to save it you can use:
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:@"MyFile"];
And to retrieve it:
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:@"MyFile"];
Updated to reflect Joe's input
Problem
I am sort of new to developing view-based iPhone applications, and I need to download this "txt" file off the internet and save it into the documents folder of the app. Can anyone show me simply how I can do this? The txt file is of a tiny size, so I wouldn't need any User interface objects... Thanks, Kevin