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)
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.
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!)
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.
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.
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
}
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With