NSURLRequest超时IOS

Cod*_*sen 5 iphone nsurlconnection nsurlrequest ipad ios

我需要使用UIRequest设置超时15秒或30秒,但它始终采用默认值.有没有办法设置连接的最小超时.

Ere*_*şel 11

这个答案解释timeoutIntervalNSURLRequest对象的最小值.如果您需要一个较小的值,那么您可以通过启动具有所需时间的NSTimer并在计时器的触发方法中取消NSURLConnection对象的连接.如:

//....
connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
[request release];

[connection start];

if (timer == NULL) {

    timer = [NSTimer scheduledTimerWithTimeInterval: TimeOutSecond
                                             target: self
                                           selector: @selector(cancelURLConnection:)
                                           userInfo: nil 
                                            repeats: NO];
    [timer retain];
}


- (void)cancelURLConnection:(NSTimer *)timerP {
    [connection cancel]; //NSURLConnection object
    NSLog(@"Connection timeout.");
    [timer invalidate];
}
Run Code Online (Sandbox Code Playgroud)