0

这篇文章这篇文章解决了在移动 Safari 中使用 JavaScript 打开 WKWebView 链接的问题,但由于某种原因,提供的解决方案不起作用。

下面的代码包含了不同解决方案的元素,但单击按钮后仍然无法打开链接。

事实上,webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy)甚至userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)不被调用。

HTML:

<html>
    <style type="text/css">
        #button { width: 100px; height: 50px; background: red; }
    </style>

    <div id="button">CLICK TO GO</div>

    <script type="text/javascript">

    var button = document.getElementById("button");
    button.addEventListener("click", doTest);

    function doTest() {
        window.open("http://www.google.com", target="_blank");
        button.innerHTML = "HEY";
    }

    </script>
</html>

视图控制器:

class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView?
    let webViewContentController = WKUserContentController()

    override func viewDidLoad() {
        super.viewDidLoad()

        let config = WKWebViewConfiguration()
        config.userContentController = webViewContentController

        webView = WKWebView(frame: view.bounds, configuration: config)

        webView!.navigationDelegate = self
        webView!.uiDelegate = self  //must have this

        var fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"Mooncake.html", ofType: nil)!)
        webView!.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

        view.addSubview(webView!)
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    }

    // this handles target=_blank links by opening them in the same view
    func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
        if navigationAction.targetFrame == nil {
            let url = navigationAction.request.url!
            let urlString = url.description.lowercased()

            if urlString.contains("http://") || urlString.contains("https://") || urlString.contains("mailto:") {
                UIApplication.shared.openURL(url)
            }
        }
        return nil
    }

    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        if navigationAction.targetFrame == nil {
            webView.load(navigationAction.request)
        }
    }
}
4

1 回答 1

0

问题是使用从 SO 答案中提取的过时的函数定义,

webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView!

webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void)

使用类文档定义的 Swift 3 函数定义解决了这个问题。

于 2016-10-26T06:40:18.733 回答