iPad/objective-C synchronous HTTP request example?

I can only find asynchronous iPad/objective C HTTP examples. How do I do a synchronous web request?

Answers


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.


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

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... :)


Need Your Help

CodeIgniter: "The filetype you are attempting to upload is not allowed."

php codeigniter mime-types codeigniter-2

I'm experiencing a very odd upload problem. Here's the relevant view file:

Javascript for..in looping over arguments ie.for( arg in arguments) does not work in IE8 but it works in Chrome 8

javascript internet-explorer foreach arguments

I faced this strange situation where foreach like construct of javascript does not work in IE but it works in FF. Well not all for..in just this special funciton does not work. I will post the code.