0

I am creating a web browser. I have a text box where the user can input where they want to go. I need to figure out how to update the text box when the user goes to another link inside of a link. For example if the user wants to go to YouTube, and they click on a video. I want the desiredWebsite or the NSTextField to be updated with the current website. I red multiple tutorials saying that it has something to do with the delegate. I do not know how to do that.

import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet weak var webview: WKWebView!
@IBOutlet var desiredWebsite: NSTextField!
@IBOutlet var goBackButton: NSButton!
@IBOutlet var goHomeButton: NSButton!
@IBOutlet var goForwardButton: NSButton!
override func viewDidLoad() {
    super.viewDidLoad()
    let initialurl = "https://google.com"
    let url = URL(string: String(initialurl))
    let request = URLRequest(url: url!)
    webview.load(request)
    desiredWebsite.stringValue = initialurl
}
@IBAction func desiredWebsiteOnTouch(_ sender: Any) {
    let gotoweb = desiredWebsite.stringValue
    let url = URL(string: String(gotoweb))
    let request = URLRequest(url: url!)
    webview.load(request)
    desiredWebsite.stringValue = gotoweb
}
@IBAction func goHomeOnTouch(_ sender: Any) {
    let gotohome = "https://google.com"
    let url = URL(string: String(gotohome))
    let request = URLRequest(url: url!)
    webview.load(request)
    desiredWebsite.stringValue = gotohome
}
@IBAction func goBackOnTouch(_ sender: Any) {
    let gotoback = ""
    let url = URL(string: String(gotoback))
    let request = URLRequest(url: url!)
    webview.load(request)
    desiredWebsite.stringValue = gotoback
}
override var representedObject: Any? {
    didSet {
    }
}
}
Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Larson Carter
  • 153
  • 2
  • 11

0 Answers0