WKWebView function for detecting if the URL has changed

前端 未结 3 929
孤城傲影
孤城傲影 2020-12-03 11:19

Is there a function for the WKWebView class that allows you to detect whenever the URL of that WebView has changed?

The didCommit and didStartProv

相关标签:
3条回答
  • 2020-12-03 11:39

    Swift Version

    // Add observer
    webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
    
    // Observe value
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let key = change?[NSKeyValueChangeKey.newKey] {
            print("observeValue \(key)") // url value
        }
    }
    
    0 讨论(0)
  • 2020-12-03 11:42

    You may add an observer:

    [webView_ addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionNew context:NULL];

    and the corresponding method that gets called when the URL changes:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context

    0 讨论(0)
  • 2020-12-03 11:54

    What do you mean they don't always seem to fire? What kind of elements? They have to in order for the WkWebView to work.

    Your first indication that the URL is trying to change is in: decidePolicyForNavigationAction

    - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction *) navigationAction decisionHandler: (void (^)(WKNavigationActionPolicy)) decisionHandler {
        NSLog(@"%s", __PRETTY_FUNCTION__);
        decisionHandler(WKNavigationActionPolicyAllow); //Always allow
        NSURL *u1 = webView.URL;
        NSURL *u2 = navigationAction.request.URL; //If changing URLs this one will be different
    }
    

    By the time you get to: didStartProvisionalNavigation It has changed.

    - (void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation {
        NSLog(@"%s", __PRETTY_FUNCTION__);
        NSURL *u1 = webView.URL;  //By this time it's changed
    }
    

    All you'd have to do is implement these delegate methods (in Swift) and do what you want when you see it change.

    0 讨论(0)
提交回复
热议问题