Bronze Challenge WKWebView Problem


#1

Hi everyone. I think I’m going crazy. By many accounts (i.e., other than variable names, this looks pretty close to other solutions), the following code should work. Unfortunately for me, I get a blank screen. That is, my WebViewController is hooked up to the TabController, I don’t get any errors while compiling or running, my messages to the console are printed out correctly, but the screen is white.

Thoughts?

import Foundation
import WebKit
import UIKit

class WebViewController: UIViewController  {
    
    var webView: WKWebView!

    override func loadView()
    {
        super.loadView()
        webView = WKWebView()
        view = webView
        let url = URL(string: "http://www.bignerdranch.com")
        let request = URLRequest(url: url!)
        webView.load(request)
        print("Loaded request")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("WebViewController loaded its view")
    }
    
} 

It’s probably something dumb, but I’ve been trying to figure it out for over an hour now, and I’m stumped.


#2

Make the webView a child (subview) of the view.


#3

Thanks for the suggestion! I don’t understand why that would make a difference, but I would like to. Can you point me to something in the book that I might have missed?

I’ve replaced “view = webView” with “view.addSubview(webView)”.
Unfortunately, the only difference I see is that instead of an empty white screen, I get an empty black screen.


#4

Okay, let’s restart.

First up. Does the code you posted compile at all?

Now, the real problem. It took me a while to see what’s actually going on.

The culprit is the url scheme.

The transport layer, by default, is simply rejecting connections to a nonsecure site. Use a secure url instead.

The following code should work. Note how the url uses the https: scheme.

import UIKit
import WebKit

class ViewController: UIViewController {

    var webView: WKWebView!
    
    override func loadView()
    {
        super.loadView()
        
        webView = WKWebView ()
        view = webView
        let url = NSURL (string: "https://forums.bignerdranch.com")
        let request = NSURLRequest (URL: url!)
        webView.loadRequest (request)
        print ("Loaded request")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print ("WebViewController loaded its view")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

#5

Have you associated the WebViewController class with the view controller on the canvas?
Previously I had the same problem getting blank screen. After associating the WebViewController class with the view controller, it works.

You can follow the steps on page 91.
Open Main.storyboard and select the Web’s view controller.
Open its identity inspector and change the Class to WebViewController

import UIKit
import WebKit

class WKWebViewController: UIViewController {

var webView: WKWebView!

override func loadView() {

    webView = WKWebView()
    
    view = webView
    
    let url = NSURL(string: "https://bignerdranch.com")!
    let request = NSURLRequest (url: url as URL)
    webView.load (request as URLRequest)
}

override func viewDidLoad() {
    print("WebViewController loaded its view.")
}

}


#6

I was having the exact same problem.

I changed my URL from http://www.bignerdranch.com to https://forums.bignerdranch.com, and suddenly it worked.

Frustrated that it wouldn’t accept a plain old http:// URL in the first place, as the Apple Developer WKWebView documentation indicated. Though this only promised http://www.apple.com would work – and it does.

Why didn’t the documentation say anything about the transport layer :frowning: .