添加TimeOut?

时间:2011-10-03 18:13:11

标签: iphone objective-c ios4 xcode4

  

可能重复:
  NSURLConnection timeout?

我有这段代码:

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);

有没有办法添加超时,所以如果请求超过1分钟就会超时?

1 个答案:

答案 0 :(得分:0)

我建议你试试ASIHTTPRequest。它是iOS的常见网络连接的包装器。它拥有您需要的一切,包括超时和发生故障时的多次连接尝试。 http://allseeing-i.com/ASIHTTPRequest/

您可以设置全局超时:

   [ASIHTTPRequest setDefaultTimeOutSeconds:5];

然后调用URL:

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

您有超时(或响应)时可以使用的委托功能

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

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