6

I have a WKWebView loading a page in iOS. If the user navigates to another page in the WKWebView, I can get the URL 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, clickedUrl = http://www.youtube.com, which is great. HOWEVER... If the user then clicks on a video - nothing. And another video - nothing. I do not get the URL of the clicked video. The same happens on Vimeo and various other sites.

I have done some research on here and found other posts, and it would seem this has something to do with 'WKWebView does not report finish of loading on non main frames', but no explanation of how to get at the URL that has been loaded. I am guessing that this has something to do with sites like YouTube are using JavaScript to navigate to other pages on their site and load them.

I have also tried webView(_:decidePolicyForNavigationAction:). This function is more verbose, but this function also does not detect the URL of links clicked within sites like YouTube. What I want to do is get the URL of everything the user clicks. I am guessing I need to go deep into NSURL or NSURLRequest to get this, but I cannot work out how to do this. Any help would be greatly appreciated.

Apologies... Please see duplicate/similar question: Detect when WKWebView has finished loading EVERY time

Community
  • 1
  • 1
AT3D
  • 857
  • 1
  • 8
  • 13
  • Hi Andre, did you fixed this issue? – Vishnu Kumar. S Jul 13 '15 at 09:03
  • Unfortunately not. I went back to UIWebView to progress with my project. Will try again with it when the next release of Swift comes along to see if it has been fixed. I have raised it as an issue on the WKWebView support forum on Apple. – AT3D Jul 24 '15 at 09:19
  • Possible duplicate of [Detect when WKWebView has finished loading EVERY time](http://stackoverflow.com/questions/29603010/detect-when-wkwebview-has-finished-loading-every-time) – Eric Aya Nov 01 '15 at 12:18
  • Thanks Eric D. ... It does say that in the question. I had only just joined the site when I posted these - I know how things work now! – AT3D Nov 01 '15 at 13:20

2 Answers2

2

You will need to observe WKWebViews URL KVO 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 WKWebViews URL property as you normally would inside observeValueForKeyPath:ofObject:change:context.

s1ris
  • 101
  • 7
-1

Use the following method to get the trigger url:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, 
    change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
    if let tapLinkURL = webView.url{ print("print URL \(tapLinkURL)") }
}
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Subbu
  • 1
  • 1
  • 1