开发者

iPad/objective-C synchronous HTTP request example?

开发者 https://www.devze.com 2022-12-27 07:27 出处:网络
I can only find asynchronous iPad/objective C HTTP e开发者_运维百科xamples.How do I do a synchronous web request?NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL];

I can only find asynchronous iPad/objective C HTTP e开发者_运维百科xamples. How do I do a synchronous web request?


NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];


Agree with h4xxr and I would forward you to

http://allseeing-i.com/ASIHTTPRequest/

Which is a fantastic lib that has robust HTTP request methods for both synch and asynch complete with code samples.


Depends on what data you're after. Something simple like this is synchronous, and is handy from time to time:

NSURL *url = [NSURL URLWithString:@"http://someaddress.asp?somedatarequest=1"];
NSArray *dataArray = [NSArray arrayWithContentsOfURL:url];

(Equivalent also exists for Dictionaries)

In this case, the system will wait for a response from someaddress.asp - therefore best perhaps to put something like this into a background thread.

If you have control over the format of the data at the other end, this can be a quick and easy way to get data into an iPhone/iPad app...

Edit - just wanted to state the obvious that typically asynchronous is usually best! No waiting around tying up system resources, especially if remote server has died etc... :)

0

精彩评论

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

关注公众号