【问题标题】:Open a WKWebview target="_blank" link in Safari在 Safari 中打开 WKWebview target="_blank" 链接
【发布时间】:2015-08-16 16:38:50
【问题描述】:

我正在尝试让我的混合 IOS 应用程序使用 Swift 和 WKWebviews 打开具有 target="_blank" 的链接,或者如果 URL 包含 http://https://mailto: 在 Mobile Safari 中。

来自this answer 我得到了这个代码。

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

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

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.sharedApplication().openURL(requestUrl)
}

如何将这两者结合起来并让它们发挥作用?我需要在 ViewController 声明中添加什么才能使其工作?

【问题讨论】:

    标签: ios swift wkwebview


    【解决方案1】:

    斯威夫特 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)
        }
    }
    

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这似乎不适用于使用 JavaScript 打开的链接,即window.open(url, "_blank")
    • 收到错误:cannot assign type viewcontroller to type wkuidelegate,用class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {更正
    • 我还可以确认 javascript 的 window.open() 对我有用
    • 当添加self.webView.uiDelegate = self 我得到Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value。我在上面有var webView: WKWebView!
    【解决方案3】:
    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】:

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

      【讨论】:

      • 出于好奇,let urlString = url!.absoluteString 是做什么的?变量urlString 初始化后在任何地方都没有被引用。
      • 经过长时间的奋斗,我找到了真正需要的东西。非常感谢。
      • 这似乎不适用于使用 JavaScript 打开的链接,即window.open(url, "_blank")。它对你有用吗?
      • WKNavigationType.LinkActivated 由任何链接点击触发,不仅在target="_blank" 时触发。这个 sn-p 将在 Safari 中打开所有链接。
      【解决方案5】:

      在 (from here)

       override func loadView() {
          super.loadView()
          self.webView.navigationDelegate = self 
          self.webView.UIDelegate = self  //must have this
       }
      

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

      func webView(webView: WKWebView,
          createWebViewWithConfiguration configuration: WKWebViewConfiguration,
          forNavigationAction navigationAction: WKNavigationAction,
          windowFeatures: WKWindowFeatures) -> WKWebView? {
              if navigationAction.targetFrame == nil {
                  var url = navigationAction.request.URL
                  if url.description.lowercaseString.rangeOfString("http://") != nil || url.description.lowercaseString.rangeOfString("https://") != nil || url.description.lowercaseString.rangeOfString("mailto:") != nil  {
                      UIApplication.sharedApplication().openURL(url)
                  }
              }
              return nil
      }
      

      【讨论】:

      • 不确定是否隐含,但为了使self.webView.UIDelegate = self 在 iOS 10 中工作,您的 ViewController 需要继承 WKUIDelegate。即class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate
      • 我已添加此代码并针对 iOS 10 和 Swift 3 进行了更新(有关转换后的代码,请参阅我的答案)。 createWebViewWith configuration: WKWebViewConfiguration, 由于某种原因没有被调用。我没有loadView 函数,但我已经在viewDidLoad 函数中添加了self.webView.uiDelegate = self。我错过了什么?
      • 这似乎不适用于使用 JavaScript 打开的链接,即window.open(url, "_blank")。它对你有用吗?
      • 我一个月前才用过这个。现在已经不行了。
      猜你喜欢
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 2012-01-19
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多