skip to Main Content

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


  1. Chosen as BEST ANSWER

    Calling this function before decisionHandler(.cancel) works fine:

    func configureAlert() {
    
            let ac = UIAlertController(title: "Page blocked!", message: nil, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            
            if webView.isLoading == false {
                present(ac, animated: true)
                return
            }
    
        }
    

  2. Try

    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
                }
              }
            }
    
           let ac = UIAlertController(title: "Page blocked!", message: nil, preferredStyle: .alert)
           ac.addAction(UIAlertAction(title: "OK", style: .cancel))
           present(ac, animated: true)
    
            decisionHandler(.cancel)
          }
    
    Login or Signup to reply.
  3. 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

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        let url = navigationAction.request.url
        
        if let host = url?.host{
            //websites in websites array = ["apple.com", "bbc.com", "youtube.com", "tiktok.com", "yahoo.com", "ufc.com"]
            for website in websites{
                if (host.contains(website) && website != "tiktok.com") && (host.contains(website) && website != "youtube.com") {
                    decisionHandler(.allow)
                    return
                }else if (host.contains(website) && website == "tiktok.com") || (host.contains(website) && website == "youtube.com") {
                    let alert = UIAlertController(title: "Blocked website", message: "You are not allowed to access website", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Dismiss...", style: .cancel))
                    present(alert, animated: true)
                    decisionHandler(.cancel)
                    return
                }
            }
        }
        decisionHandler(.cancel)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search