Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView issues with tel: links

I've looked though a lot of the answers for this question but they seem to be outdated now and none of the solutions are working for me and just give lots of errors.

I'm just gettign into xcode and apps and am loading a local html file "index.html" into a WKWebView. This is fine, loads fine, displays as it should, BUT I have some tel: links on the page which don't work, I tap on them and nothing. I am using fraework7 for my html files and have added the "external" class, which works in safari etc.

UPDATE: Using the following code I can click on a link and it will try and call, but then gives a fatal error

import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate{

    @IBOutlet weak var webview: WKWebView!
    override func viewDidLoad() {
        webview.navigationDelegate = self
        super.viewDidLoad()
        let htmlpath = Bundle.main.path(forResource: "index", ofType: "html")
        let url = URL(fileURLWithPath: htmlpath!)
        let request = URLRequest(url: url)
        webview.load(request)
        webview.scrollView.bounces = false
        webview.configuration.dataDetectorTypes = .phoneNumber
        // Do any additional setup after loading the view, typically from a nib.
    }
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
        if navigationAction.request.url?.scheme == "tel" {
            UIApplication.shared.openURL(navigationAction.request.url!)
            decisionHandler(.cancel)
        }
        decisionHandler(.allow)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

and html looks like:

          <tr>

            <td>Police / Fire / Ambulance</td>

            <td>Emergency Services</td>

            <td><a href = "tel:999" class = "external">999</a></td>

          </tr>

obviously part of a larger table but this is the example.

Any pointers would be greatly appreciated as I've been going round in circles for ages.

like image 305
Cerbster Avatar asked Mar 05 '18 12:03

Cerbster


People also ask

What browser does WKWebView use?

WKWebView uses the Nitro JavaScript engine, also used by mobile Safari, which comes with significant performance improvements over UIWebView's older JavaScript engine.

How do I refresh WKWebView?

The WKWebView already contains a scrollview. All you need to do is create the refresh control, assign a target function that will get called when a user initiates a refresh, and attach the refresh control to the scrollview.

Is WKWebView secure?

The WKWebView is a modern API applying all the modern web security mechanisms, it's still maintained by Apple and gets updates. The good thing about WKWebView is that it does out-of-process rendering, so if the attackers find a memory corruption vulnerability in it, your application's process is still isolated.

How do I import an HTML file into WKWebView?

Load Local HTML File to a WKWebViewlet myUrl = myProjectBundle. url(forResource: "my-html-file", withExtension: "html")!


1 Answers

Managed to find a solution finally:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate{

  @IBOutlet weak var webView: WKWebView!

  override func viewDidLoad() {
    webView.navigationDelegate = self
    webView.uiDelegate = self
    super.viewDidLoad()
    let htmlpath = Bundle.main.path(forResource: "index", ofType: "html")
    let url2 = URL(fileURLWithPath: htmlpath!)
    let request = URLRequest(url: url2)
    webView.load(request)
    webView.scrollView.bounces = false
    //webview.configuration.dataDetectorTypes = .phoneNumber
  }

  func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.request.url?.scheme == "tel" {
        UIApplication.shared.openURL(navigationAction.request.url!)
        decisionHandler(.cancel)
    } else {
        decisionHandler(.allow)
    }
}

Using this seems to pick up the tel: links fine, an answer I found on here previously didn't have the else statement so fired decisionHandler more than once which created the error, the else statement fixed that and now seems fine.

like image 137
Cerbster Avatar answered Sep 23 '22 04:09

Cerbster