NSURLRequest Timeout IOS
Asked Answered
C

3

5

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.

Coition answered 30/7, 2012 at 9:0 Comment(2)
Please post the code you are using.Farthermost
I found answer in this link as I had the same problem and it worked for me. kelp.phate.org/2012/07/set-timeout-nsurlrequest.htmlEugenle
D
12

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];
}
Deenadeenya answered 30/7, 2012 at 12:51 Comment(0)
P
3

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

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

Instead set it AFTER construction:

request.timeoutInterval = 70;

Also note that there seem to be some limitations to how low you can set the interval. Read this post for more information: https://devforums.apple.com/message/108087#108087

Prayer answered 30/7, 2012 at 9:9 Comment(4)
If I set the timeout more than 240 it is working fine. I need a less than 240 sec.Coition
240 seems to be the lowest accepted value (see link in answer above). However, some report success by setting the timeout interval after construction (perhaps the rule is not in forced in that case). But this might change in the future. Maybe you should consider another approach. 15 sec on a slow cellular network might be problematic...Prayer
in iOS7 and iOS8 you can set any value you want. It defaults to 60sSumerian
@AndrewBennett: i am able to set any range of values for time out in ios 7 and 8, but is not sticking to the specified value. Say if i set a time out interval of 180 seconds, the didFailWithError: gets triggered ranging from 75 to 220 seconds and some times greater than this , but not at the interval of 180 seconds. I am making asynchronous web service calls with POST method. Any idea on this?Schmitz
D
1

POST requests have a timeout minimum which is 4 minutes, I believe. The most secure way is to start a NSTimer and cancel the request when the timeout fires.

Dysfunction answered 30/7, 2012 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.