Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set time out in NSURLRequest in iphone

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?

like image 376
sri Avatar asked Jan 11 '11 05:01

sri


2 Answers

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.

like image 92
Oleg Danu Avatar answered Nov 09 '22 13:11

Oleg Danu


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!

like image 38
faterpig Avatar answered Nov 09 '22 11:11

faterpig