Android WebViewClient 的 shouldInterceptRequest 方法:在 iOS 中是否有任何等效方法?

Android WebViewClient's shouldInterceptRequest method: is there any equivalent of that in iOS?

在Android中,我可以使用WebViewClient的shouldInterceptRequest方法来自定义WebView对某些URL的处理。但是,我在 iOS 的 UIWebView 和 UIWebViewDelegate 中找不到任何等效项。有什么方法可以实现与 iOS 中的 shouldInterceptRequest 相同的目的,即允许我 return 资源请求的数据?

我想做的是让web视图支持一些私有图片格式(见问题:)。

欢迎任何建议。

在 UIKit 中没有完全相同的方法,但是您可以通过实现 -[UIWebViewDelegate webView:webView:shouldStartLoadWithRequest:navigationType:] 来获得相同的效果。在此方法中,您可以调用 -[UIWebView loadData:MIMEType:textEncodingName:baseURL:] 来加载您自己的数据,然后调用 return NO,这样网络视图就不会尝试加载它自己的数据。

NSURLProtocol 是正确的方法。

请参阅以下参考资料: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial, http://objectivetoast.com/2014/05/19/intercepting-requests-with-nsurlprotocol/, and more.