KINWebBrowser is an open source web browser module for iOS apps. I recently upgraded KINWebBrowser to use WKWebView to begin phasing out UIWebView. This yields significant improvements, but:
Problem: WKWebView does not enable users to launch links containing URLs for phone numbers, email address, maps, etc.
How can I configure a WKWebView to launch the standard iOS behaviors for these alternate URLs when launched as links from the displayed page?
All of the code is available here
More info on WKWebKit
See the issue on the KINWebBrowser GitHub here
The culprit is HTML attribute target set to value _blank which is used to signal links that should be opened in new tab instead of the current one. This target setting is meant to tell the browser (which is WKWebView in this case) that this link should be open in new tab by default. Which is the root of the problem.
You need to turn the string into a URL , then put the URL into an URLRequest , and WKWebView will load that. Fortunately it's not hard to do! Warning: Your URL must be complete, and valid, in order for this process to work. That means including the https:// part.
Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.
Step-1: Firstly, create a new project with a Single View App. Step-2: Then, open “Main. storyboard” and on your ViewController's view drag “Webkit View”. Select a WKWebView and place it on your view of a view controller.
I was able to get it to work for the Google Maps link (which appears to be related to the target="_blank") and for the tel: scheme by adding this function to your KINWebBrowserViewController.m
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { if(webView != self.wkWebView) { decisionHandler(WKNavigationActionPolicyAllow); return; } UIApplication *app = [UIApplication sharedApplication]; NSURL *url = navigationAction.request.URL; if (!navigationAction.targetFrame) { if ([app canOpenURL:url]) { [app openURL:url]; decisionHandler(WKNavigationActionPolicyCancel); return; } } if ([url.scheme isEqualToString:@"tel"]) { if ([app canOpenURL:url]) { [app openURL:url]; decisionHandler(WKNavigationActionPolicyCancel); return; } } decisionHandler(WKNavigationActionPolicyAllow); }
Works on xcode 8.1, Swift 2.3.
For target="_blank", phone number (tel:) and email (mailto:) links.
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { if webView != self.webview { decisionHandler(.Allow) return } let app = UIApplication.sharedApplication() if let url = navigationAction.request.URL { // Handle target="_blank" if navigationAction.targetFrame == nil { if app.canOpenURL(url) { app.openURL(url) decisionHandler(.Cancel) return } } // Handle phone and email links if url.scheme == "tel" || url.scheme == "mailto" { if app.canOpenURL(url) { app.openURL(url) decisionHandler(.Cancel) return } } decisionHandler(.Allow) } }
Updated for swift 4.0
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if webView != self.webView { decisionHandler(.allow) return } let app = UIApplication.shared if let url = navigationAction.request.url { // Handle target="_blank" if navigationAction.targetFrame == nil { if app.canOpenURL(url) { app.open(url) decisionHandler(.cancel) return } } // Handle phone and email links if url.scheme == "tel" || url.scheme == "mailto" { if app.canOpenURL(url) { app.open(url) } decisionHandler(.cancel) return } decisionHandler(.allow) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With