I've posted a more in-depth question to try and get to the bottom of the issue, but in a brief:
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 #keyPath(WKWebView.url)
:
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")
}
}
However, the output to console is always the same:
URL Change: # No value provided
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 my other question, 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.
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?
Trying to obtain the URL value from webView.url
seems to always return nil.
EDIT: I am able to get the exact URL value with the observerValue function code:
if let key = change?[NSKeyValueChangeKey.newKey] {
print("URL: \(key)") // url value
}
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://")?