NSURLRequest IOS timeout

I need to set a timeout of 15 seconds or 30 seconds using UIRequest, but it always takes a default value. Is there a way to set a minimum timeout for a connection.

+5
source share
3 answers

This answer explains the minimum value of the timeoutIntervalobject NSURLRequest. If you need a lower value, you can do this by running NSTimer at the right time and in the timer firing method, you will disconnect the connection of your NSURLConnection object. How 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];
}
+11
source

There seems to be a problem with setting the timeout interval property at build time:

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:240.0];

Instead, install it AFTER building:

request.timeoutInterval = 70;

, , , , . : https://devforums.apple.com/message/108087#108087

+3

POST requests have a minimum timeout, which, it seems to me, is 4 minutes. The safest way is to start NSTimerand cancel the request when the timeout is triggered.

+1
source

All Articles