Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching phone/email/map links in WKWebView

Tags:

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

like image 809
dfmuir Avatar asked Oct 22 '14 06:10

dfmuir


People also ask

Why is WKWebView not opening links with target _blank?

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.

How do I load a url in WKWebView swift 5?

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.

Is WKWebView deprecated?

Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.

How do I migrate from UIWebView to WKWebView?

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.


2 Answers

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); } 
like image 78
Darren Ehlers Avatar answered Sep 25 '22 12:09

Darren Ehlers


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)     }  } 
like image 32
Victor Almeida Avatar answered Sep 24 '22 12:09

Victor Almeida