Given the code:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
if let host = url?.host {
for website in websites {
if host.contains(website) {
decisionHandler(.allow)
return
}
}
}
decisionHandler(.cancel)
}
I wrote the following block of code, but it didn’t work correctly no matter where I put it.
let ac = UIAlertController(title: "Page blocked!", message: nil, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .cancel))
present(ac, animated: true)
3
Answers
Calling this function before
decisionHandler(.cancel)
works fine:Try
Maybe i misunderstood the question. Try this out and let me know what you think.
Note: my websites array is included below as a note