Running into a wall with the bronze challenge

#1

Hey guys,

I don’t know what it is that I’m doing so wrong, but I can’t seem to get the bronze challenge to work. I have a new tab, a new View Controller with a web view element on top of it, and some code. I tried all sorts of things, using partial solutions from this forum, or tutorials from the web, but I just cannot for the life of me seem to get something to show in the web tab.

[code]import UIKit
import WebKit

class WebViewController: UIViewController {

var webView: WKWebView!

override func loadView() {
    
    webView = WKWebView()
    view = webView
    
    let url = NSURL(string: "https://www.google.com")!
    webView.loadRequest(NSURLRequest(URL: url))
    
}

override func viewDidLoad() {    

    super.viewDidLoad()
    print("WebViewController loaded its view")

}

}[/code]

This is the code that I have right now (after I threw all the things out again that didn’t work out). I figured, since it was a bronze challenge, it shouldn’t be hard to get something on screen, as was the case with the map view. Not sure what else I should try here. And this is just a bronze one. Darn.

#2

There’s nothing the matter with your code–it works for me.

  1. Do you get any warnings or errors?

  2. Do you have an internet connection on your computer? Are there any firewalls, proxies, or other special circumstances?

Try quitting the Simulator, then re-running your program.

#3

Hey man, appreciate the response.

I get no warnings, nor errors. I just get a blank screen on that tab, the others work fine. So I assume I may have messed something up in that department. Internet should be working fine. Firewall is the standard one for mac (I’m on an iMac), and I checked to make sure XCode wasn’t blocked.

Not sure if this is allowed, but in case it is, I zipped the project file and uploaded it. Maybe someone sees what I’m missing here.

https://www.wetransfer.com/downloads/535833210c58ae96ba16e105a8117b4120160317203625/648030f19ff51b087222266632f25aff20160317203625/205ef2

#4

After changing a few settings (iOS 9.1 operating system, iPhone 6s Simulator), your app works fine for me–if I change the url to:

…which means the server at the url you used:

doesn’t understand the https protocol, e.g. your app is speaking German to a server that only understands French. When I use Safari to try to go to the url you used, I get:

I’m wondering why you posted code in your question which contains the url “https://www.google.com”, yet your actual code uses a different url? You can’t get relevant help if you don’t post your actual code.

#5

Sorry mate, I actually tried it with the google link as well, was messing around with that, but you got an earlier version I guess. Still doesn’t work for me though. Still blank screen on the web tab. My simulator is iOS 9.2, guess maybe that’s it?

#6

Try setting the Deployment Target to iOS 9.1. Select the top entry in the Project Navigator. Then at the top of the middle pane, on the far left there is a little square with a bar down the left hand side. Click on that to reveal a little side menu, then select each of the World Trotter entries in the side menu and set the Deployment target to iOS 9.1. I don’t know why there are two different settings for the iOS version.

If that doesn’t work, then I think the Simulator must be having a problem using your internet connection.

#7

I did exactly as you said, but still nothing. I have the same problem when I run the app on my iPhone. So it may be an internet connection issue, but it’s not limited to the simulator.

#8

I would try these in order:

  1. Simulator>Quit, then run your app in Xcode.
  2. In Xcode, Product>Clean, then Product>press Option key>Clean Build Folder, then run your app in Xcode.
  3. Simulator>Resent Content and Settings, then run your app in Xcode.
  4. Open Applications/Utilities/Console.app, select system.log, then run your app and see what messages are logged.
  5. See if any of the suggestions here will work:
    stackoverflow.com/questions/2512 … s-internet

What version of Xcode are you running and what version of OSX are you running?

#9

Hello Inkidu,

I ran into exactly the same problem as you did and the very strange and rather silly solution that worked for me was to copy and paste the URL from the browser into the code. Surprisingly, although the pasted URL reads exactly the same as the one I typed before, I can now even retype it to display any other website.

#10

Same problem. Looks like the book doesn’t tell you that only https URLs will work with web kit. https://www.hackingwithswift.com/read/4/2/creating-a-simple-browser-with-wkwebview had a comment that pointed this out to me.

In info.plist

<key>NSAppTransportSecurity
<dict>
<!-- Changing to true allows all connections (DANGER) use at your own risk–>
<key>NSAllowsArbitraryLoads</key>
<false />
</dict>

1 Like
#11

In the Main.storyboard, you need to select the web view controller, go to identity inspected and then selected the WebViewController as the class, so it is linked to your new SWIFT file

1 Like
#12

I had a similar issue, and I realized that I did not change the class. Once I changed it, it worked!

Thank you @faranqureshi

I also tried to load some other websites, which normally don’t have https://, but added that part to them, and they did not work.

There are a few more things that differ in my code (which worked for me), too.

  1. I included the main part of the code in the overridden viewDidLoad() function and not loadView()
  2. I used webView = WKWebView(frame: view.bounds) and when I tried to remove the (frame: view.bounds), nothing showed up
  3. At the very bottom, but inside the viewDidLoad() function, I added the view as a sub view: view.addSubview(webView)

I hope this helps

1 Like
#13

This did it for me. Thanks @don1941

#14

Dude, that fixed it. Thanks. It was driving me nuts! http indeed does not work, but https does.