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)
}
asked Aug 24, 2022 at 14:30
4
  • 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) Commented Aug 24, 2022 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. Commented Aug 25, 2022 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? Commented Aug 25, 2022 at 17:33
  • Right! but at the same time process all possible cases and not just as I did using linkActivated. Commented Aug 29, 2022 at 8:48

1 Answer 1

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.

answered Aug 29, 2022 at 19:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.