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

Project 4 Easy Browser Challenge 1 (HELP ME IM LOSING MY MIND)...lol

Forums > Swift

@aDayu  

Hello,

In the code below I am creating an alert that's supposed to pop up everytime a user clicks a URL that isn't allowed. However, everytime I run my code the alert pops up in the initial view controller, and keeps on poping up even after I press return. How do I show this alert only when user clicks wrong URL?

I have been at this for hours, please......HELP ME!!!!!

func webView(_ webView: WKWebView, decidePolicyFor navigationAction:
                    WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        let url = navigationAction.request.url

        if let host = url?.host { //host means web domain like nba.com
            for website in websites {
                if host.contains(website) {
                    decisionHandler(.allow)
                    return
                }
            }
        configAlert()
        }

        decisionHandler(.cancel)

    }

    func configAlert() {
        let vc = UIAlertController(title: "ERROR 404", message: "Sorry this page is blocked", preferredStyle: .alert)
        vc.addAction(UIAlertAction(title: "Return", style: .cancel))
        present(vc, animated: true, completion: nil)

//        if webView.isLoading == false {
//            present(vc, animated: true)
//            return
//        }
    }

3      

Hi,

there is a small mistake, your code to display alert is inside the if let, which means that it will execute each time you try to load new website.

3      

Good attempt. just update the code according to @nemecek-filip.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.