fetch Data from a server [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionI need a simple function in objective C to get data from a webserver, something like the php function file_get_contents
, I dont need to submit any POST data so its gona be simple.
Thanks
Depends a bit on the contents of the data you want to retrieve. As an example, if the data can be interpreted as a UTF8-encoded string you could use:
NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
If you just want to get bytes of data without interpreting as a string you can also do this:
NSData *data = [NSData dataWithContentsOfURL:url];
You then have all the data available to parse out however you need to.
The NSData class has a method 'dataWithContentsOfURL' that will enable you to retrieve data from a webserver.
Sample code:
NSData* someData = [NSData dataWithContentsOfURL: [NSURL URLWithString:someURL]];
Official documentation: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
精彩评论