16

I want to get the url of a tapped link in WKWebView. The links are in a custom format that will trigger certain actions in the app. e.g. http://my-site/help#deeplink-intercom. I am using KVO like so:

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

This works great when the link is tapped on the first time. However if I tap the same link twice in a row it won't report the link tap (obviously because the actual value hasn't changed). Is there a workaround to fix this so I can detect every tap and get the link? Any pointer on this would be great! Thanks!

Kex
  • 8,023
  • 9
  • 56
  • 129

3 Answers3

28

You can use WKWebView delegate method. And don't forget to set the webview delegate to 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)
}
Borzh
  • 5,069
  • 2
  • 48
  • 64
Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49
  • 8
    This won't work as this method is never called. The links I am tapping on do not issue HTTP requests. – Kex Jun 12 '17 at 08:50
  • It was changed to func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) – Nikolay Tabunchenko Sep 24 '21 at 07:51
14

Change addObserver like this

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

In observeValue function you able get both value

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])
    }
}
Bala
  • 1,224
  • 1
  • 13
  • 25
  • Just what I was looking for! Thanks for your help! – Kex Jun 07 '17 at 13:22
  • I think key value observing should not really be used any more unless there are no other options. WKWebView provides a delegate for all this behavior (see Borzh comment). For that I think @Borzh his reply should be the accepted answer. – Saren Inden Nov 07 '19 at 06:28
2

Swift 5.0

Remember to set navigationDelegate on the WKWebView instance.

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