StanLe
StanLe

Reputation: 5137

Read a .txt file from the web - Objective-C

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.somewebsite.com/textfile.txt
else
  NSLog(@"user has no internet");

Can someone show me code to do this?

Upvotes: 4

Views: 1537

Answers (3)

PengOne
PengOne

Reputation: 48398

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

Upvotes: 1

Rahul Vyas
Rahul Vyas

Reputation: 28720

Check NSURLConnection classes.

Upvotes: 0

Nick B
Nick B

Reputation: 1101

Check out this library to download files via http: http://allseeing-i.com/ASIHTTPRequest/

Upvotes: 2

Related Questions