Open a WKWebview target=“_blank” link in Safari

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

    Code updated for iOS 10 Swift 3:

    override func loadView() {
        super.loadView()
        self.webView.navigationDelegate = self 
        self.webView.uiDelegate = self  //must have this
    }
    
    func webView(_ webView: WKWebView,
                   createWebViewWith configuration: WKWebViewConfiguration,
                   for navigationAction: WKNavigationAction,
                   windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil, let url = navigationAction.request.url {
          if url.description.lowercased().range(of: "http://") != nil ||
            url.description.lowercased().range(of: "https://") != nil ||
            url.description.lowercased().range(of: "mailto:") != nil {
            UIApplication.shared.openURL(url)
          }
        }
      return nil
    }
    

提交回复
热议问题