Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

cocoa touch - How to download a text file with iPhone SDK?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...