WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 26. challenge - tasks 1 & 3 - The Great Struggle

Forums > 100 Days of Swift

Hi guys,

I've been struggling whole day with the two tasks from day 26. challenge and you're my last hope.

When it comes to task 1, which particularly is:

"If users try to visit a URL that isn’t allowed, show an alert saying it’s blocked."

What I did here was:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        let url = navigationAction.request.url
        let ac = UIAlertController(title: "Page blocked", message: "", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .cancel))
        if let host = url?.host {
            for website in websites {
                if host.contains(website) {
                    decisionHandler(.allow)
                    return
                } else {
                    decisionHandler(.cancel)
                    present(ac, animated: true)
                    return
                }
            }
        }
        decisionHandler(.cancel)
    }

Unfortunately, what this thing does is it shows the alert every time I attempt to enter any other website than the one that initially loaded when the app launched (which, in my instance, is 'apple.com'), no matter what website I try to enter. Maybe the problem comes from the fact that I can't fully grasp what the "host" in the code is and whether it 'contains' a website? I'm just confused.

Task no. 3:

"For more of a challenge, try changing the initial view controller to a table view like in project 1, where users can choose their website from a list rather than just having the first in the array loaded up front."

Oh boy, how much I screwed that one up. I'm totally lost with the relation between the detail view controller and the view controller, I tried to somehow connect them to make it work but I had to scrap everything, as the code started to turn into an array of a million errors. What's surprising though, it didn't pose that much of a problem in Day 23. Milestone. Here, however, it's one big clustertruck. If anyone was willing to share some time to explain what should be done here in a few steps with some important side notes where needed, I would be very grateful.

Have a nice weekend y'all!

1      

Apparently being stubborn enough is all what it takes, cause I solved it :D Of course I'll be more than happy to help if anyone has a similar problem to mine!

1      

Well done! I'd really appreciate if you could help me with the answer also

1      

I am absolutely having this problem and I can't tell if it's the www. that's tacked on the beginning of "www.apple.com" when it comes back from the host but that can't be it... cause hacking with swift works both ways...

1      

hey @MateusZ I'm super curious how you fixed this? I'm getting the same thing and I can't find the fix for it... our code isn't EXACTLY alike (I don't have the else in my if statement around the decisionHandler but otherwise it's the same)

1      

Call this function right before decisionHandler(.cancel):

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      

Hi guys i made this, and works. Hope i can help anyone in the same situation:

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 showUnsecureSiteAlert = UIAlertController(title: "Site not allowed", message: nil, preferredStyle: .alert)
        showUnsecureSiteAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        present(showUnsecureSiteAlert, animated: true)
        decisionHandler(.cancel)
    }

1      

Hey @MateusZ did we need a detailVC for this one?

I was simply trying to change the viewcontroller to a tableviewcontroller, and on table rowdidtap or whatever (sorry im in bed so from memory) compiling the url to open as opposed to doing one hard coded.

Can it not be done this way?

1      

Well... it was rather challenging task to complete (the task 1 at least, the rest two were just fine) because of a small and persisting bug. Strange enough though, as I initially wrote the code by myself as many mentioned here by putting alert controller before decisionHandler(.cancel) but alert persisted dispite i choose a site from allowed list. The result that worked for me, and so far works as expected i.e. if I try to click on links that lead to other sites it denies to open and shows the propert alert, was posted by @abdullatif. You don't really need to write a function though, but simply put that line before decisionHandler(.cancel)

  let deniedAC = UIAlertController(title: "Alert", message: "URL is not allowed.", preferredStyle: .alert)
    deniedAC.addAction(UIAlertAction(title: "Cancel", style: .destructive))

    if !webView.isLoading {
      present(deniedAC, animated: true)
    }
    decisionHandler(.cancel)

Hope it will be useful for someone who is struggling with that task...

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.