UIWebView and timeouts

十年热恋 提交于 2019-12-07 00:34:47

问题


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


回答1:


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:




回答2:


I figured out that the UIWebView does not use the same NSURLRequest over and over again. So my answer would be, that it's not possible to globally configure a timeout for the UIWebView.



来源:https://stackoverflow.com/questions/5788237/uiwebview-and-timeouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!