Open a WKWebview target=“_blank” link in Safari

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

    func webView(_ webView: WKWebView,
               createWebViewWith configuration: WKWebViewConfiguration,
               for navigationAction: WKNavigationAction,
               windowFeatures: WKWindowFeatures) -> WKWebView? {
      if navigationAction.targetFrame == nil, let url = navigationAction.request.url, let scheme = url.scheme {
        if ["http", "https", "mailto"].contains(where: { $0.caseInsensitiveCompare(scheme) == .orderedSame }) {
          UIApplication.shared.openURL(url)
        }
      }
      return nil
    }
    

提交回复
热议问题