0

I added a WKWebView to my main View, but for some reason when I run the app it gives me a whitescreen. Do I need to set some additional size variables to the view or webView variable?

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        view.frame = view.bounds
        webView = WKWebView(frame: CGRect( x: 0, y: 20, width: 380, height: 150 ), configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        view.addSubview(webView)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://www.google.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}
soia019192
  • 43
  • 2
  • 8

2 Answers2

0

The first line in documentation of UIViewController.loadView says:

Creates the view that the controller manages

Which means, there is no mainView in loadView, here you have to assign/load a view for the UIViewController.

So, you have to replace this line:

view.addSubview(webView)

With this line:

self.view = webView

Anyways, here is your fixed view controller that is using loadView:

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .null, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        self.view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://www.google.com")!
        let myRequest = URLRequest(url: myURL)
        webView.load(myRequest)
    }
}

Notice that I changed frame: .null, this too is managed by the ViewController.

Here is another class that shows you how to use addSubview instead of loadView to do the same

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView = WKWebView(frame: CGRect(x: 0, y: 20, width: 380, height: 150))
        self.webView.navigationDelegate = self
        self.webView.uiDelegate = self
        self.view.addSubview(webView)

        if let url = URL(string: "https://www.google.com") {
            self.webView.load(URLRequest(url: url))
        }
    }
}
AamirR
  • 11,672
  • 4
  • 59
  • 73
0

loadView()

You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property

You can try with viewDidLoad

import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
        var webView: WKWebView!           
        override func viewDidLoad() {
            super.viewDidLoad()
            let webConfiguration = WKWebViewConfiguration()
            view.frame = view.bounds
            webView = WKWebView(frame: CGRect( x: 0, y: 20, width: UIScreen.main.bounds.width, height: 250 ), configuration: webConfiguration)
            webView.navigationDelegate = self
            webView.uiDelegate = self
            view.addSubview(webView)
            let myURL = URL(string: "https://www.google.com")
            let myRequest = URLRequest(url: myURL!)
            webView.load(myRequest)
        }
    }
Jack
  • 13,571
  • 6
  • 76
  • 98