0

For example, I write as follows, then it works correctly, that is, the content that comes is displayed as it should and when the user clicks the link, it doesn't redirect anywhere.

extension AuthorizationContentView: WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated {
            decisionHandler(.cancel)
        } else {
            decisionHandler(.allow)
        }
    }
}

BUT I need to cover all the cases of clicking on the link and not just linkActivated(A link with an href attribute was activated by the user), but if I write just decisionHandler(.cancel), then the content is not displayed and it is unclear why so.

extension AuthorizationContentView: WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(.cancel)
        return
    }
}

UPD:

func setupWebView(content: String) {
    let supportDarkCSS = "<style>:root { color-scheme: light dark; }</style>"

    contentStackView.removeAllArrangedSubviews()
    webView.loadHTMLString(content + supportDarkCSS, baseURL: nil)
    contentStackView.addArrangedSubview(webView)
}
Morozov
  • 4,968
  • 6
  • 39
  • 70
  • Is the link predefined or has a certain pattern? How do you plan to identify the relevant link and not block others? you might need to parse and check the URL from within the action request (i.e WKNavigationAction.URLRequest) – CloudBalancing Aug 24 '22 at 21:50
  • @CloudBalancing the html date comes to me and there may be any links/scripts in it and I need to prohibit clicking on them in any case, I updated the code, namely the `setupWebView` method. Check please, maybe it will be clearer this way. – Morozov Aug 25 '22 at 10:46
  • So the issue is that if you block everything the page does not load - correct? So you wish to be able to load the page and block navigation actions afterward? – CloudBalancing Aug 25 '22 at 17:33
  • Right! but at the same time process all possible cases and not just as I did using `linkActivated`. – Morozov Aug 29 '22 at 08:48

1 Answers1

0

Ok, so after I understand what you are trying to do, the .linkActivated navigation type does not include all of the cases you are trying to block.

I think the best options is to keep intercept the requests using the func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) delegate function.

You can use the URLRequest property on the WKNavigationActioni.e navigationAction.request.

Then you can use the URLRequest metadata, for example, the URL itself (navigationAction.request.url) or the HTTP method, and understand if that request needs to be blocked.

CloudBalancing
  • 1,461
  • 2
  • 11
  • 22