I can't tell if I am misunderstanding the expected behavior of WKNavigationDelegate or if something is misconfigured or broken.
My goal is to load the initial web content in my WKWebView, but then intercept link-clicks within that content and (at least sometimes) open those in Safari, as described in these forum posts:
WKWebView - Open external link websites in Safari
https://developer.apple.com/forums/thread/125641
Open WKWebView Links in Safari
https://developer.apple.com/forums/thread/68427?answerId=199512022#199512022
Here's my simplified delegate method code. Because it gets called on the initial request load, it checks against a copy of the original URL and if it matches calls decisionHandler(.allow).
(I'd hoped to be able to use .navigationType instead of checking the URL, but it comes in as .other on first load, which seems too vague to always allow).
If it's any other URL, I call decisionHandler(.cancel) and attempt to open with UIApplication open().
This doesn't work however because the delegate method is only called once, on the WKWebiew's initial load() - on that call the URLs match and .allow is returned. The delegate method is not called at all for the click on the link. (FWIW the link is a relative href in the source HTML; it is to a different page not just a hashtag on the same page).
Can anyone clarify how this is supposed to work? It makes no sense to me to have the delegate control the initial load() - if I didn't want to show the content in the WebView, I wouldn't load the URLRequest.
It makes sense for me to have the delegate callback invoked for subsequent navigation, but that isn't happening.
My goal is to load the initial web content in my WKWebView, but then intercept link-clicks within that content and (at least sometimes) open those in Safari, as described in these forum posts:
WKWebView - Open external link websites in Safari
https://developer.apple.com/forums/thread/125641
Open WKWebView Links in Safari
https://developer.apple.com/forums/thread/68427?answerId=199512022#199512022
Here's my simplified delegate method code. Because it gets called on the initial request load, it checks against a copy of the original URL and if it matches calls decisionHandler(.allow).
(I'd hoped to be able to use .navigationType instead of checking the URL, but it comes in as .other on first load, which seems too vague to always allow).
If it's any other URL, I call decisionHandler(.cancel) and attempt to open with UIApplication open().
Code Block func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { Swift.print(#function) if let navActionURL = navigationAction.request.url { if navActionURL == self.originalURL { decisionHandler(.allow) } else { decisionHandler(.cancel) UIApplication.shared.open(navActionURL, options: [:], completionHandler: nil) } } }
This doesn't work however because the delegate method is only called once, on the WKWebiew's initial load() - on that call the URLs match and .allow is returned. The delegate method is not called at all for the click on the link. (FWIW the link is a relative href in the source HTML; it is to a different page not just a hashtag on the same page).
Can anyone clarify how this is supposed to work? It makes no sense to me to have the delegate control the initial load() - if I didn't want to show the content in the WebView, I wouldn't load the URLRequest.
It makes sense for me to have the delegate callback invoked for subsequent navigation, but that isn't happening.