0

I am trying to understand the request interception problem in UIWebView so I can start looking into WKWebView.

What I don't understand is, UIWebView has its own method for interception which is shouldStartLoadWithRequest, but it seems like most of people are still implementing a custom NSURLProtocol to do interception. I am curious why most people choose not to use UIWebView's method? What are the differences here?

Anna
  • 443
  • 9
  • 29
  • Possible duplicate of [UIAlertController custom font, size, color](https://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color) – Fantini Dec 13 '18 at 21:59

1 Answers1

0

If what you want to do is simply load or reject the request, you can do this in WKWebView by becoming its WKWebViewNavigationDelegate and implementing webView(_:decidePolicyFor:decisionHandler:)

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    let request = navigationAction.request

    let policy: WKNavigationActionPolicy
    if request.path.contains("something_bad") {
        policy = .cancel
    } else {
        policy = .allow
    }
    decisionHandler(policy)
}
Procrastin8
  • 4,193
  • 12
  • 25
  • I understand, but for my understanding this class only intercepts the url, doesn't intercept any resources and Ajax requests. I am wondering if UIWebViews shouldStartLoad is the same – Anna Dec 14 '18 at 14:50
  • The method on question only captures page loads, not other resource loads. Chances are those folks want to intercept image loads, script loads, etc. – dgatwood Dec 14 '18 at 21:20