In my iPhone app, how can I download a .txt file from the web, and then get it's contents into an NSString. For instance, say the text file was located at http://www.somewebsite.com/textfile.txt
Then what I want to do is:
if(user has internet connection)
NSString *textFile = download text file from http://www.som开发者_运维百科ewebsite.com/textfile.txt
else
NSLog(@"user has no internet");
Can someone show me code to do this?
Check out this library to download files via http: http://allseeing-i.com/ASIHTTPRequest/
To test for internet connectivity, look at Apple's Reachability
example. It has all the code you'll need for that.
To get the file, you may want to check out this SO post: Can I make POST or GET requests from an iphone application? and its relevant follow-up post: Making GET and POST Requests from an iPhone Application - Clarification Needed
Check NSURLConnection classes.
精彩评论