title: How to obtain URL value of KVO #keyPath(WKWebView.url)?
body: <p>I've posted a <a href="https://stackoverflow.com/questions/60175052/issue-with-observing-wkwebview-url-changes-via-javascript-events">more in-depth question</a> to try and get to the bottom of the issue, but in a brief:</p>
  
  <p>I'm attempting to show a PHP/JS-based web application (Laravel) through a WKWebView. However, due to the nature of the script's redirecting properties, the only code I've gotten to actually detect the URL change is with <code>#keyPath(WKWebView.url)</code>:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      webView.navigationDelegate = self
      webView.uiDelegate = self    
      webView.addObserver(self, forKeyPath: #keyPath(WKWebView.url), options: .new, context: nil)
  }
  
  override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      if keyPath == #keyPath(WKWebView.url) {
          print("URL Change:", self.webView.url?.absoluteString ?? "# No value provided")
      }
  }
  </code></pre>
  
  <p>However, the output to console is always the same:</p>
  
  <p><code>URL Change: # No value provided</code></p>
  
  <p>So I know that the KVO for WKWebView.url is able to fire upon script-based redirection within the WebView. In fact, if you take a look at <a href="https://stackoverflow.com/questions/60175052/issue-with-observing-wkwebview-url-changes-via-javascript-events">my other question</a>, it is the only code that can detect this sort of redirection – which is strange, because when launched in Safari (both iOS and macOS), the URL bar is able to reflect those redirected changes to the URL's value. However, when in the WKWebView, none of the WKNavigationDelegate functions are able to detect such a change to the URL.</p>
  
  <p>Is there any way to obtain the URL directly from the keyPath value of WKWebView.url when fired? Are there any alternatives, not described in my previously-mentioned question, that could obtain the URL?</p>
  
  <p>Trying to obtain the URL value from <code>webView.url</code> seems to always return nil.</p>
  
  <p>EDIT: I am able to get the exact URL value with the observerValue function code:</p>
  
  <pre><code>if let key = change?[NSKeyValueChangeKey.newKey] {
      print("URL: \(key)") // url value
  }
  </code></pre>
  
  <p>However, I am unable to cast it as a String or pass it to another function otherwise. Is there any way to set this key as a variable, if it .contains("https://")?</p>