本文介绍了NSURLRequest超时IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to set timeout 15sec or 30 sec with UIRequest, but it always takes default one. Is there any way to set minimum timeout to connection.

推荐答案

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

This answer explains about the minimum value of timeoutInterval of an NSURLRequest object. If you need a smaller value, then you may do so with starting an NSTimer with the desired time and in the firing method of the timer, you cancel the connection of your NSURLConnection object. As in:

//....
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];
}

这篇关于NSURLRequest超时IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 16:26