Adding A TimeOut? [duplicate]
Possible Duplicate:
NSURLConnection t开发者_开发百科imeout?
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?
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
}
精彩评论