UIWebView and timeouts

flohei picture flohei · Apr 26, 2011 · Viewed 10.1k times · Source

I'm using a UIWebView to display some content. Within this content the user can navigate through a bunch of sites which are being loaded using AJAX. One step on this sites requires quite some time. Since I didn't find anything about a timeoue for UIWebView I tried to set it on the initial NSURLRequest it loads.

Unfortunately this does not seem to do anything. I'm not quite sure if I'm supposed to set the timeout myself? Is there anything I'm missing? Does the UIWebView override my timeout? I'm a little bit confused on this so any help would be appreciated. :)

Best
–f

Update

I built a test project to verify my "theory" on how the UIWebView works with timeouts. Here is the setup:

  • a PHP file called sleep.php that has a parameter with which I can configure the time to sleep until it sends a result
  • a little Xcode project with a single view which is a webView that loads the sleep.php like this:

    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:10]; [webView loadRequest:request];

So, while playing with the seconds parameter I could see that the UIWebView works as it should. I get either webViewDidFinishLoad: or webView:didFailLoadWithError: depending on the timeout.

After verifying that the timeoutInterval works as advertised I brought in a proxy.php (that has two links to the sleep.php configured with 5 and 15 seconds) to see if my request configuration is still valid for new requests. I changed the initial NSURLRequest to use the proxy.php but left the 10 seconds configuration for the timeout.

When I start the app now the two links are being displayed. When I click the first one it loads as expected after 5 seconds. Unfortunately, the second link loads as well after 15 seconds.

So my first attempt to reconfigure the NSURLRequest would be to change it in webView:shouldStartLoadWithRequest:navigationType:. Unfortunately again, the web service works with AJAX so the delegate method does not get called.

Any other suggestions? Is there a way to globally configure the timeout value for the whole UIWebView?

Best
–f

Answer

Ahmad Kayyali picture Ahmad Kayyali · Apr 26, 2011

You might be interested in watching the UIWebView delegate webViewDidFinishLoading with your own custom NSTimer.

Also if you're having an issue till the server get you the response you can set the timeout for NSURLConnection on requestWithURL:cachePolicy:timeoutInterval: