Bronze Challenge (another tab) solution

this took forever as the view wasn’t loading because I set the url to “http://…” instead of “https://…”

import UIKit
import WebKit

class WebViewController: UIViewController{
    
    var webView : WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        webView = WKWebView()
        let myURL = URL(string:"https://www.bignerdranch.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        view = webView
    }
}

also you must remember to link the class in the storyboard

How do you link a new class in the storyboard so you can embed it in the tabBarController?

@stevelhunt If I understand your question correctly you have a view controller and you want to connect it to the tab bar controller. Since I am not sure I fully understand you question I am going to make some assumptions.

  1. You have a swift file with a view controller
  2. You have a view controller setup in the storyboard canvas
  3. You setup a tab bar controller

If these are not true they are your first steps.
Next, control drag from the tab bar controller to the new view controller.
Select view controller from the popup menu.
You are done.
It is a pretty simple task but not very intuitive IMHO.
Hope I was able to help and if I misunderstood anything please let me know.

Your suggestion worked. I wrote the view controller functions in code and forgot that I had to create a physical view controller in the storyboard and link it to the code file!!

Thanks,

Steve

Hi. I think my connections are right, and my code is similar to the code above, but I have a black screen on the simulator. Could you tell me what could be wrong?

Ok. I hadn’t changed my custom class)

Here’s my solution going off the documentation at developer.apple.com
The only other thing was to ctrl-drag from tab bar controller to the new view controller in storyboard and update the bar item text, then make sure that the new view controller is wired to custom class: class = WebViewController in my case in the identity inspector.

The code creates the view programmatically, so there’s nothing to drag into the new view controller.

import UIKit
import WebKit

class WebViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string: "https://www.bignerdranch.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

1 Like

What is the difference between UIWebView and WKWebView? I did following it also work! And also no need WebKit!!

import UIKit
//import WebKit

class WebViewController: UIViewController {

@IBOutlet var myWebView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    myWebView = UIWebView()
    
    let url = URL(string: "https://www.bignerdranch.com")
    let urlRequest = URLRequest(url: url!)
    myWebView.loadRequest(urlRequest)
    
    view = myWebView
    
}