Get the URL of all items used with WKWebView

I have a WKWebView loading a page in iOS. If the user goes to another page in WKWebView, I can get the URL that they clicked as follows:

var clickedUrl = String() func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { clickedUrl = webView.URL?.absoluteString as String! // GETS URL OF CURRENT PAGE LOADED } 

If the user goes to YouTube, click "Url = http://www.youtube.com , and that's great. HOWEVER ... If the user then clicks on the video, nothing. And another video, nothing. I don't get the URL clicked video: The same thing happens on Vimeo and other sites.

I did some research here and found other posts, and it seems to have something to do with "WKWebView does not report download completion not for main frames", but does not explain how to get the URL that was loaded. I guess this has something to do with sites like YouTube, using JavaScript to go to other pages on their site and load them.

I also tried webView(_:decidePolicyForNavigationAction:) . This feature is more verbose, but this feature also does not detect the URLs of links that can be found on sites such as YouTube. What I want to do is get the URL of everything the user clicks. I guess I need to delve deeper into NSURL or NSURLRequest to get this, but I cannot figure out how to do this. Any help would be greatly appreciated.

Apologies ... See duplicate / similar question: Detecting when WKWebView finished loading EVERY time

+6
source share
1 answer

You will need to see the WKWebViews KVO URL property:

Swift:

 webView.addObserver(self, forKeyPath: "URL", options: .New, context: nil) 

Objective-C:

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

And then access the WKWebViews URL property, as usual, in watchValueForKeyPath: ofObject: change: context.

+2
source

Source: https://habr.com/ru/post/984884/


All Articles