TAGS :Viewed: 7 - Published at: a few seconds ago

[ Read Text file in iphone ]

I want to read text file that is placed on server. for example my file is placed on this link: http://mysite.com/files/objects.txt How can I read the contents of this file into NSString?

Answer 1


Try this:

NSError* e;
NSString* str = [NSString stringWithContentsOfURL:@"http://mysite.com/files/objects.txt" encoding:NSASCIIEncoding error:&e]
if(e != nil) {
  //There was an error
}
//Everything good

Answer 2


Note the syntax has changed with recent iOS updates...

NSError* err;
NSURL* url = [NSURL URLWithString:@"<your URL goes here>"];
NSString* str = [NSString stringWithContentsOfURL:url
                                             encoding:NSASCIIStringEncoding
                                                error:&err];