Migrating UIWebView delegate to WKWebView delegate method Migrating UIWebView delegate to WKWebView delegate method ios ios

Migrating UIWebView delegate to WKWebView delegate method


You might required to implement the following in your code, which means instead of using UIWebViewDelegate protocol try to use WKNavigationDelegate protocol. I guess you are missing one of the most important function when you are handling with sessions.

   func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {        print(#function)        completionHandler(.performDefaultHandling,nil)    }

There are different types of AuthChallengeDisposition , like

public enum AuthChallengeDisposition : Int {    case useCredential    case performDefaultHandling    case cancelAuthenticationChallenge    case rejectProtectionSpace}

Complete WKNavigationDelegate protocols are

  extension ViewController: WKNavigationDelegate{    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {        print(#function)    }    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {        print(#function)    }    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {        print(#function)    }    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {        print(#function)    }    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {        print(#function)    }    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {        print(#function)    }    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {        print(#function)    }    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {        print(#function)        completionHandler(.performDefaultHandling,nil)    }    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {        print(#function)        decisionHandler(.allow)    }    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {        print(#function)        decisionHandler(.allow)    }}


I guess you can use webView(_:decidePolicyFor:decisionHandler:) and you block/cancel or allow requests. That should work in the same way.

Disclaimer: I haven't tested that yet, I'll do so as soon as I find some time.


analyzing your code, i have found a statement that is never reached cause of "return" called before.

The statement is:

decisionHandler(.allow)

You can find it as last line of code for the function:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void)

that you have up this method:

func webViewDidStartLoad(_ webView: UIWebView) {    numberOfDidStartLoads += 1}