How to set time out in NSURLRequest in iphone
Asked Answered
M

2

9

I'm trying to disconnect or stop loading URL request in 15 secs, when internet connection get disconnected. i tried with setTimeoutInterval in NSURLRequest, which is not working. can any know how to resolve the issue?

Misusage answered 11/1, 2011 at 5:31 Comment(0)
P
14

Here is an example for you:

NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:link]         
               cachePolicy:NSURLRequestUseProtocolCachePolicy  
               timeoutInterval:20.0];

Here you have a 20 sec timeout interval.

Premiership answered 11/1, 2011 at 7:30 Comment(2)
I tried timeoutInterval:20.0, but it is not working for me. is anything needs to be done?Misusage
the timeoutInterval is ignored & set to 240s if the call is a POST request. See: devforums.apple.com/message/108087#108087Nica
F
2

From what I have experienced, this example from Oleg will not work. I guess you're facing the same problem as me and from what I have read online, maybe this solution will work for you too.

Using NSTimer, set the timer to start at the webViewDidStartLoad delegate method, and then make sure that you have counters at the webViewDidFinishLoad and webViewDidStarLoad methods to check if the webView has been loaded (this is an ugly hack, but unless you use javascript to check the number of elements in your page, there is really no way to know how many times these 2 methods will be called).

After doing so, do a check of the counters at the method that you are running after the timer has reached its time, and that should work.

The NSTimer code is here --> http://www.iphonedevsdk.com/forum/iphone-sdk-development/14403-nstimer-examples.html

Hope this helps, it did for me. Although I know there are flaws, it works for me as I am loading a splash screen which is supposed to be lightweight in the first place. I'm still looking for solutions though, will post edits here if I have other alternatives.

Feedback is greatly welcomed!

Fondue answered 22/6, 2012 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.