开发者

Adding A TimeOut? [duplicate]

开发者 https://www.devze.com 2023-04-10 02:07 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: NSURLConnection t开发者_开发百科imeout?
This question already has answers here: Closed 10 years ago.

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
}
0

精彩评论

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