Bronze Challenge: Another Tab (programmatic and non-programmatic approach)

// Non-programmatic approach:

/* ----------------------------------- My add to the Bronze Challenge: Another Tab ------------------------------------ */

import UIKit

class WebViewController: UIViewController {

@IBOutlet var webView: UIWebView!

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

}

/* -------------------------------------------------------------------------------------------------------------------- */

// Programmatic approach:

/* ----------------------------------- My add to the Bronze Challenge: Another Tab ------------------------------------ */

import UIKit
import WebKit

class WebViewController: UIViewController, WKNavigationDelegate {

var webView: WKWebView!

override func loadView() {
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

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

}

/* -------------------------------------------------------------------------------------------------------------------- */

2 Likes

recommended from Apple Developer Documentation:
// rename ViewController to WebViewController

import UIKit
import WebKit
class ViewController: 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.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}

Can confirm that @rolfino 's Programmatic approach works, as long as you set up everything correctly in Interface Builder with the Tab Controller and new View Controller & you also create the new Swift file for the new view controller appropriately (and change the class name in the Identity Inspector - I used a different class name than @rolfino ). I also changed the Tab Bar Item Title. However, I did not add an icon like the ones that were provided for Convert & Map from the book resources.

tab%20bar%20item%20title

custom%20class

This tutorial was helpful for me on this challenge: “Creating a simple browser with WKWebView”

Great. But I have a question. How could we scroll the screen up and down? It seems that it does not work…Thanks