开发者

fetch Data from a server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_运维百科

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

I 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜