I try to catch the url about to load in WKWebView before it loads. Based on documents decidePolicyFor navigationAction (WKNavigationDelegate) should do the job but my problem is this delegate gets called after new url get loaded not before that.
here is the extension I wrote.
extension MyWebViewController: WKNavigationDelegate {
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let navigationURL = navigationAction.request.url else {
decisionHandler(.allow)
return
}
let forbiddenUrlPattern = Configuration.current.links.forbiddenUrlPattern
if forbiddenUrlPattern.matches(url: navigationURL) {
decisionHandler(.cancel)
showFullScreenError(error: .forbidden)
return
}
// Default policy is to allow navigation to any links the subclass doesn't know about
decisionHandler(.allow)
}
}
*PS the matches extension checks the pattern and it works fine. now the problem is that the content of forbiddenUrl showed for a time before this delegate func gets called and then the error page comes to screen, and if I close it the last visible webPage is from forbidden link pattern.
is there any way to understand about the link before actually loading it in webView?
I am using Xcode 11.2.1 & Swift 5.0
.cancelcondition is met the redirect it not executed, and no new content is shown. So I can confirm that this setup, in principle, is working as you originally expected it to work. Also (to test the basic setup) if you just putdecisionHandler(.cancel); returnin the delegate function I would be very surprised if you could still see any content loading at all. Good luck! - GammadidCommitNavigationfunction instead since this isn't working properly? You could also try settingwebView.isOpaque = falseandwebView.backgroundColor = UIColor.clearuntil you have verified the URL is not forbidden. - elliott-io