3

Possible Duplicate:
NSURLConnection timeout?

I have this code:

NSURL *url = [NSURL URLWithString:@"http://some-url-that-has-to-work/"];
    NSURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSHTTPURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request
        returningResponse:&response error:NULL];
    return (response != nil);

Is there a way to add a timeout so if the request takes more then 1 minute it just times out?

Community
  • 1
  • 1
user964627
  • 655
  • 3
  • 14
  • 24

1 Answers1

0

I would suggest you try ASIHTTPRequest. It's a wrapper for common network connections for iOS. It has everything you need, including timeout and multiple connection attempts in case of failures. http://allseeing-i.com/ASIHTTPRequest/

You can set a global timeout as such:

   [ASIHTTPRequest setDefaultTimeOutSeconds:5];

and then call the URL:

NSURL *url = [NSURL URLWithString:@"http://www......."];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

you have delegate functions that you can use when you get a timeout (or the response)

- (void)requestFinished:(ASIHTTPRequest *)request {
// this is called if the request is successful
}

- (void)requestFailed:(ASIHTTPRequest *)request {
// this is called if the request fails
}
aporat
  • 5,922
  • 5
  • 32
  • 54
  • ASI is a lot of bloat to add to your project just to be able to set the timeout policy when NSURLConnection does the job just fine. – Mark Adams Oct 03 '11 at 20:32
  • @aporat no point in switching to `ASIHTTPRequest` just for setting timeout. Infact underlying ASIHTTPRequest is `NSURLConnection` ! – Srikar Appalaraju Oct 04 '11 at 03:59