5

In Android, I can use WebViewClient's shouldInterceptRequest method to custom WebView's handling of certain URL. However, I cannot find anything equivalent in iOS's UIWebView and UIWebViewDelegate. Is there any way I can achieve the same purpose of shouldInterceptRequest in iOS, i.e., allow me to return data for a resource request?

What I want to do is to make the web view support some private image formats(see the question: Can I make Android WebView support other image formats (e.g. TIFF)?).

Any suggestion is appreciated.

Community
  • 1
  • 1
shengbinmeng
  • 1,517
  • 2
  • 12
  • 22

2 Answers2

0

There's not a method exactly like that in UIKit, but you can get the same effect by implementing -[UIWebViewDelegate webView:webView:shouldStartLoadWithRequest:navigationType:]. In this method you can call -[UIWebView loadData:MIMEType:textEncodingName:baseURL:] to load your own data and then return NO so the web view doesn't try to load it's own data.

Dash
  • 17,188
  • 6
  • 48
  • 49
  • The methods you mentioned are called for the whole page and not every resource (like ) in the page, which means I have to parse and reproduce the whole page before loading. Am I right? – shengbinmeng May 30 '15 at 05:12
  • It's called for sections of the page, I'm not sure for every image but I imagine it would be since each image requires a separate url request. Give it a try and let us know! – Dash May 30 '15 at 05:21
  • 1
    It's called for the whole page (or frame), but not for every resource. – shengbinmeng Jun 26 '15 at 07:24
  • Yeah `shouldStartLoadWithRequest:` is the equivalent of Android's `shouldOverrideUrlLoading`, which is only called when the page URL changes, not `shouldInterceptRequest`, which is called for every resource retrieved from the browser. So this doesn't really answer the OP's question. @shengbinmeng's answer is correct. – davidgoli Dec 02 '15 at 03:47
  • Addendum: NSURLProtocol does not work consistently with WKWebView. See http://stackoverflow.com/questions/24208229/wkwebview-and-nsurlprotocol-not-working – davidgoli Dec 02 '15 at 04:01