2

I'm working with an app that makes extensive use of UIWebViews to display content loaded from the web. Most of the time this works well, but every once in a while (particularly on 3G) a connection problem will disrupt loading.

When it's the main webview, this isn't a problem because webView:didFailLoadWithError: is called on the delegate. However, the page HTML includes some and tags that reference other web resources, and very occasionally the initial request will go through but one of these subsequent requests will fail.

It seems like I ought to be able to trigger handling for these failures with an NSURLProtocol subclass, but in initWithRequest:cachedResponse:client:, I can't find a way of determining which UIWebView fired off the request (so that I can notify its delegate to display something to the user). Nor can I find a delegate method that gets called when one of these requests is fired (webView:shouldStartLoadWithRequest:navigationType: only gets called for window.location.href assignments and similar main document requests, not secondary requests). What's the simplest way to detect that one of those secondary requests has failed (and which URL it is trying to load, and which UIWebView is making it)?

Arkaaito
  • 7,347
  • 3
  • 39
  • 56
  • Related question with no answers: http://stackoverflow.com/questions/13061776/correlate-nsurlrequest-with-uiwebview – Arkaaito May 09 '13 at 01:50
  • We are having the same problem. There is content within our HTML (images, etc.) pulled from Amazon S3 and sometimes they do not load. We need to be able to detect when this happens. – Gerry Aug 18 '14 at 17:52

1 Answers1

0

I was also getting similar issues with intermittent failure to show cached html references to .css and .js urls.

Turns out, my problem was the CachePolicy was set to "ignore cache" for the reference items, and thus invalidated my "canUseProtocol" delegate logic.

My solution was to return YES for canUseProtocol in my caching protocol subclass when offline, then checking for caching policy when network is reachable only.

In your case, I would think you can at least validate the urls loading in the canUseProtocol method, even if you dont get the callback for them if they fail. Perhaps you can use your own custom protocol that avoids downloading the asset by default, then downloads it individually with its own callback to inject it into the webview on success?

Hope this helps!

Lytic
  • 786
  • 5
  • 12