|
I got to end of project number 4 and iv been trying to solve the first challenge of this project . Q: users try to visit a URL that isn’t allowed, show an alert saying it’s blocked. |
|
|
|
My temporary "solution" was to add a "unallowedwebsite" string in the websites array and check if the website url contain the "unallowed" word. Actually there aren't url added by the user, so I think that this could be a good way to make this challenge. In future projects you will learn how to let users insert text but you have to wait some days... ;-) |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I actually tried to add some if else statement in the openPage func , I dont know if i do it correctly .
|
|
Hi Eric. You need to implement the following method:
which is a function of the WKNavigationDelegate (which your class needs to conform to). Before loading a page, the web view will call this function so you can decide what to do before the page loads. That's why your logic should be here. In that function, you will loop through the list of allowed sites and compare them to your URL's host.
If it is a match, you'll allow the site to load:
Otherwise (after the loop), You'll show the error message and send
Hope that helps. |
|
Following up with the below: I actually solved this. all i did was to move the alert one " } " above.. so it would look like this instead, and the alert worked fine.HOWEVER I am actually not sure of the reason why it has to be one " } " above and why it would work now.
Hi all. I am having another problem with this challange as well I have put the alert inside of the decisionHandler , and the alert works fine. However the alert shows regardless of the website url. I used the "Open" button to load another site , like apple.com( which is an allowed site) and when i click on apple.com link in my button , the alert shows up and the site still loads.
|
|
Hi teekachu, I was having the same problem. I added some print statements after the decicionHandler calls, like
and
Now you will see that some sites that are referenced try to make additional page loads within the html/css/js. Obviously a lot of sites do this without you being aware of it... So I guess the coding is functioning ok: in case of the apple site it is allowed to go there and the page starts loading. During that load you get an alert on an attempted load within that page that isn't allowed. For instance, if you have "hackingwithswift.com" in your allowed websites array, you will see that the pages that have an embedded youtube reference wil load, but with a warning and without the youtube reference visible. |
|
Hi guys, @offgrid, thanks so much for your explanation. I've been trying to grasp this thing for some time now, and now I fully understand how it works. What you're saying is true - indeed, the "hackingwithswift.com" subpages with an embedded YT reference do load, with a warning and without the youtube reference visible though. What makes me wonder is why some of the clickable links don't react to my interaction, only printing "allowed:". The links I'm talking about are the ones found in the "100 Days With Swift" days. For example, if I try to enter Day 24. of "100 Days With Swift (UIKit)", everything loads fine (apart from the things we mentioned above). But when I try to click/tap "Setting Up" or other 2 links, literally NOTHING happens - the link gets an underline, message "allowed:" is printed, but that's all - the page is not loading. Is it only my case, or do you guys experience the same thing? |
|
Hi @MateusZ , I'm experiencing the same. Must be because these links try to open in a new tab using target="blank" in the html code. You would probably need to have some extra swift coding to make this work, like opening the new page in a new WKWebView, but that is beyond my knowledge. So for now I just take it for what it is. |
|
|
|
I am also getting the same problem with apple.com and gmail.com. I, don't know why it is happening . For apple and gmail the alert appears after the website is loaded. I had tried printing statement to check the flow of code but in case of above mentioned sites it is going through both the print statements. Can, anyone help with this one.
|
|
Hi fellas. I figured out why your block alert would display even if, say, "hackingwithswift.com" is in the This also took me an entire afternoon to figure out. Below is my code for it.
Note that I decided to define a separate
Updated: It seems to me that when a site tries to call - say - an ad with its host different from our preset hosts, the |
|
Hello everyone! Thank you for your valuable inputs. I still have several doubts about this challenge. I realized that even with the alert that the website wasn't allowed, it kept loading so I placed several prints to check the status of my constants in every step and to check which
I believed that placing the
But guess what? I broke my code with this exception:
So I am still confused with the P.D.: Please note that there isn't a loop in my Thanks in advanced for your time! KB |
|
Hi all, as a test I've added an invalid host name at index 4 like this: var websites = ["apple.com", "botb.com", "youtube.com", "hackingwithswift.com", "twitter.co"] But when I click to open the "twitter" the error message is not showing at all, I thought this would actually work, as we also checking if it's a valid URL I guess? I also declared a method then I called this method here: func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { let url = navigationAction.request.url // Equals to the current URL of the navigation / check the current requested URL
The alert appears after on "botb.com" is loaded which I'm still trying to figure out why as it's a valid website like "apple.com" and so on. Could someone clarify this please? I see is a lot of confusion about this challange.Thank you! |
|
Also if you guys, put this code after that function call (which is our "blocked" alert) } showAlert() debugPrint("(host) URL isn't allowed!") // This will show exactly why the error message is displaying blocked }
I figured out, if I clicked on "botb.com" this was trying to acces "www.facebook.com" which is weird and have no idea what is the connection between these two as they're separate websites.But the bottom line is that you will see why your alert is showing to the user.Hopefully this might help some of you! |
|
Hey guys, I change to navigationRespone and it just work!! :) using .isForMainFrame to ignore advertise request or request that not change the webview
|
|
Using breakpoints to look at what various variables contained, I noticed what was causing the issue was a call to "about:blank", which like others mentioned I suspect is a extrenuous call being made by the page on request, not the application. When I refactored my code using @togahepi solution, the issue did appear to be solved. |
|
Hi everyone! As I was struggling to find the solution as well, I thought it would be nice to share... not sure if it is the best approach, but so far it works! So basically, I called this method from the
|
|
@togahepi Hey bro, thank you for your solution. It works perfectly! Could you tell me why did you need to keep track of iterations inside loop (var count)? |
|
Hi, I am joining ths discussion quite late, but happily. I struggled with this challenge and THINK I have come up with atleast a semi-solution. like many others, I had this code: func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
} to test it I added an URL in the openTapped func to the action that was not in the websites array: @objc func openTapped(){ let ac = UIAlertController(title: "Open page ...", message: nil, preferredStyle: .actionSheet) for website in websites { ac.addAction(UIAlertAction(title: website, style: .default, handler: openPage)) ac.addAction(UIAlertAction(title: "https://twitter.com", style: .default, handler: openPage)) }
|
|
Your code seems almost correct, but there's a minor issue in the logic. The way you're checking the validity of the URL might cause a crash if the action's title is nil. Here's an improved version of your code with added error handling for nil titles: swift Copy code func openPage(action: UIAlertAction) { guard let title = action.title, !title.isEmpty else { return // Do nothing if the action title is nil or empty }
} This should help prevent potential crashes due to nil titles and provides better user feedback when an invalid URL is provided. |
|
Your code seems almost correct, but there's a minor issue in the logic. The way you're checking the validity of the URL might cause a crash if the action's title is nil. Here's an improved version of your code with added error handling for nil titles: swift Copy code func openPage(action: UIAlertAction) { guard let title = action.title, !title.isEmpty else { return // Do nothing if the action title is nil or empty }
} This should help prevent potential crashes due to nil titles and provides better user feedback when an invalid URL is provided welpix.com |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.