Bronze Challenge (another tab) solution


#1

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
    }
}

#2

also you must remember to link the class in the storyboard


#3

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


#4

@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.


#5

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


#6

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?


#7

Ok. I hadn’t changed my custom class)


#8

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)
    }
}


#9

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
    
}

#10