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.
-
Please post the code you are using. – Jim Jul 30 '12 at 09:05
-
I found answer in this link as I had the same problem and it worked for me. http://kelp.phate.org/2012/07/set-timeout-nsurlrequest.html – Khaled Annajar Dec 11 '12 at 18:39
3 Answers
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];
}

- 1
- 1

- 1,047
- 8
- 16
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

- 3,356
- 25
- 44
-
If I set the timeout more than 240 it is working fine. I need a less than 240 sec. – Codesen Jul 30 '12 at 09:16
-
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... – EsbenB Jul 30 '12 at 09:22
-
in iOS7 and iOS8 you can set any value you want. It defaults to 60s – Andrew Bennett Oct 01 '14 at 22:27
-
@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? – XiOS Dec 16 '14 at 13:47
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.

- 128,090
- 22
- 218
- 270