UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.