TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Project 4, challenge

Forums > Swift

Hey. This is probably obvious...But:

How should i call denied() function when decitionHandler is "(.cancel)" ?

Function to check if site is permitted:

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)
        }

    }

Function to call alert:

func denied() {
        let ad = UIAlertController(title: "Permission denied", message: "Your are not allowed to CNBC", preferredStyle: .alert)
        ad.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
        present(ad, animated: true)
    }

I tried to call it right after decisionHandler(.cancel), but alert is trigged for "first loaded" website as well.

4      

// should the page be loaded?
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    let url = navigationAction.request.url
    if url!.absoluteString.range(of: "about:blank") != nil {
        decisionHandler(.cancel)
        return
    }
    //check if host which means website domain like apple.com
    if let host = url?.host {
        for website in websites {
            print("host \(host)")
            if host.contains(website) {
                decisionHandler(.allow)
                return
            }
        }
    }
    print(url!)
    let ac = UIAlertController(title: "Page is Blocked", message: nil, preferredStyle: .alert)
    //cancel will default dismiss alert controller if tapped
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    //anchors location on iPad
    //ac.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
    present(ac, animated: true)
    decisionHandler(.cancel)
}

5      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.