在Safari中打开WKWebview target =“_ blank”链接

时间:2015-06-02 18:20:38

标签: ios swift wkwebview

我正在尝试使用Swift和WKWebviews打开包含target="_blank"或网址包含http://https://或{{1}的链接的混合IOS应用在移动Safari中。

this answer我得到此代码。

mailto:

首先,这对我没有任何作用。其次,我希望它在新窗口中打开。我发现这个代码应该做那样的事情......

func webView(webView: WKWebView!, createWebViewWithConfiguration     configuration: WKWebViewConfiguration!, forNavigationAction navigationAction:     WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
    if navigationAction.targetFrame == nil {
        webView.loadRequest(navigationAction.request)
    }
    return nil
}

如何将这两者放在一起让它们起作用?我需要在ViewController声明中添加什么才能使其正常工作?

5 个答案:

答案 0 :(得分:10)

在(from here

ax.update_datalim

然后添加函数(from here, with additions)...

x = np.array([0] * y.shape[0])
fig, ax = plt.subplots()
ax.update_datalim(list(zip(x, y)), updatex=False)

答案 1 :(得分:8)

首先添加WKNavigationDelegatewebviewWk.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)
    }

答案 2 :(得分:7)

针对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
}

答案 3 :(得分:2)

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
}

答案 4 :(得分:1)

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)
    }
}