Open a WKWebview target=“_blank” link in Safari

后端 未结 5 1277
执念已碎
执念已碎 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:39

    First add WKNavigationDelegate and webviewWk.navigationDelegate = self

    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    
            //this is a 'new window action' (aka target="_blank") > open this URL externally. If we´re doing nothing here, WKWebView will also just do nothing. Maybe this will change in a later stage of the iOS 8 Beta
            if navigationAction.navigationType == WKNavigationType.LinkActivated {
                println("here link Activated!!!")
                let url = navigationAction.request.URL
                let shared = UIApplication.sharedApplication()
    
                let urlString = url!.absoluteString
    
                if shared.canOpenURL(url!) {
                    shared.openURL(url!)
                }
    
                decisionHandler(WKNavigationActionPolicy.Cancel)
            }
    
            decisionHandler(WKNavigationActionPolicy.Allow)
        }
    

提交回复
热议问题