Search code examples
ioswkwebview

iOS WkWebView - What is the difference in this situation?


I have the source code below.

WkWebview will open the app when the URL scheme has tel, sms, item- or mailto. (item and mailto are our custom scheme)

I wonder that what is the difference between using decisionHandler(.cancle) and decisionHandler(.allow) because It is the same to implement UIApplication.shared.open(URL) in both conditions.

 if scheme == "tel" || scheme == "sms" || scheme == "itms-services" {
            UIApplication.shared.open(destinationUrl)
            decisionHandler(.allow)
            return
        }
        
        else if scheme == "mailto" {
            UIApplication.shared.open(URL(string: "ncsone://mail/write"))
            decisionHandler(.cancel)
            return
        }

Solution

  • As stated in the official documentation of the WKNavigationDelegate, using the decisionHandler with the .cancel value will prevent the WKWebView from loading the content of the destinationUrl.

    This might be used to prevent the webview from navigating to an unknown URL.