【问题标题】:Getting the link URL tapped in WKWebView获取在 WKWebView 中点击的链接 URL
【发布时间】:2017-11-08 13:22:30
【问题描述】:

我想在WKWebView 中获取点击链接的网址。链接采用自定义格式,将触发应用程序中的某些操作。例如http://my-site/help#deeplink-intercom。我像这样使用KVO

override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
        view = webView
    }

 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let newValue = change?[.newKey] {
            print("url changed: \(newValue)")
        }
        print("Did tap!!")
    }

这在第一次点击链接时效果很好。但是,如果我连续两次点击相同的链接,它不会报告链接点击(显然是因为实际值没有改变)。是否有解决此问题的解决方法,以便我可以检测到每次点击并获取链接?任何关于此的指针都会很棒!谢谢!

【问题讨论】:

    标签: ios swift wkwebview


    【解决方案1】:

    您可以使用 WKWebView 委托方法。并且不要忘记将 webview 委托设置为 self:webview.navigationDelegate = self

    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
    
        switch navigationAction.navigationType {
            case .LinkActivated:
            if navigationAction.targetFrame == nil {
                self.webView?.loadRequest(navigationAction.request)// It will load that link in same WKWebView
            }
            default:
                break
        }
    
        if let url = navigationAction.request.URL {
            print(url.absoluteString) // It will give the selected link URL
    
        }
        decisionHandler(.Allow)
    }
    

    【讨论】:

    • 这不会起作用,因为这个方法永远不会被调用。我点击的链接不会发出 HTTP 请求。
    • 改为func webView(_ webView: WKWebView, decisionPolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
    【解决方案2】:

    像这样更改addObserver

    webView.addObserver(self, forKeyPath: "URL", options: [.new, .old], context: nil)
    

    observeValue 函数中你可以获得两个值

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let newValue = change?[.newKey] as? Int, let oldValue = change?[.oldKey] as? Int, newValue != oldValue {
            //Value Changed
            print(change?[.newKey])
        }else{
            //Value not Changed
            print(change?[.oldKey])
        }
    }
    

    【讨论】:

    • 正是我想要的!感谢您的帮助!
    • 我认为不应该再使用键值观察,除非没有其他选择。 WKWebView 为所有这些行为提供了一个委托(参见 Borzh 评论)。为此,我认为@Borzh 他的回复应该是公认的答案。
    【解决方案3】:

    Swift 5.0

    记得在 WKWebView 实例上设置 navigationDelegate。

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print(String(describing: navigationAction))
        decisionHandler(.allow)
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2015-06-14
      相关资源
      最近更新 更多