Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView function for detecting if the URL has changed

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

The didCommit and didStartProvisionalNavigation functions don't always seem to fire when working with certain elements within the WebView.

EDIT: Attempted adding a Notification observer. Here's what I have thus far:

extension Notification.Name {
    static let checkURL = Notification.Name("checkURL")
}

NotificationCenter.default.post(name: .checkURL, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(getter: webView.url), name: .checkURL, object: webView.url)
like image 961
Justin Bush Avatar asked Dec 18 '16 21:12

Justin Bush


People also ask

What is UIWebView WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I know if WKWebView is loaded Swift?

To check if your WKWebView has loaded easily implement the following method: import WebKit import UIKit class ViewController: UIViewController, WKNavigationDelegate { let webView = WKWebView() func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

How do I import a url into WKWebView?

You need to turn the string into a URL , then put the URL into an URLRequest , and WKWebView will load that. Fortunately it's not hard to do! Warning: Your URL must be complete, and valid, in order for this process to work. That means including the https:// part.

How do I add WKWebView to Objective C?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


3 Answers

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
    }
}
like image 97
Atalyk Avatar answered Sep 30 '22 05:09

Atalyk


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.

like image 40
hawkeyecoder Avatar answered Sep 30 '22 06:09

hawkeyecoder


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

like image 44
vomako Avatar answered Sep 30 '22 06:09

vomako