iOS WebView URL Handling/open external links in Safari 📲

Want to have a native feeling/UX in your iOS WebView app? Then you should handle external URLs differently than your regular WebView links, e.g. when clicking on a Facebook link we can open the site in Safari browser and stop loading it in WebView:

 

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

    {

if ((requestURL.host != nil) && requestURL.host! == "m.facebook.com")

             {

             loadingSign.stopAnimating()

             self.loadingSign.isHidden = true

             UIApplication.shared.openURL(requestURL)

             decisionHandler(.cancel)

             return

             }

}

If you use the WebViewGold Xcode template (or the WeSetupYourWebViewApp setup service), you do not need to worry about the whole URL Handling process. It just works, even for configurations of all external URLs (instead of specific URLs):

 

 

Any questions? Just email us! 🙂