Open a WKWebview target=“_blank” link in Safari

后端 未结 5 1284
执念已碎
执念已碎 2020-12-10 15:04

I am trying to get my Hybrid IOS app that uses Swift and WKWebviews to open a link that has target=\"_blank\" or if the URL contains http://,

5条回答
  •  渐次进展
    2020-12-10 15:33

    Swift 4.2

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            print("here link Activated!!!")
            if let url = navigationAction.request.url {
                let shared = UIApplication.shared
                if shared.canOpenURL(url) {
                    shared.open(url, options: [:], completionHandler: nil)
                }
            }
            decisionHandler(.cancel)
        }
        else {
            decisionHandler(.allow)
        }
    }
    

提交回复
热议问题