0

I have been creating my own web browser and have successfully done that in xcode. But then I wanted a certain url google for example to load when it first opens, now I have been having issues with this. If anyone can explain to me how to do this it would be great. I have tried multiple things in my ViewController class, but nothing seems to work.

Here's my code snippet, if there's something wrong with it could you tell me? :) thanks

import Cocoa
import WebKit

class ViewController: NSViewController {
    @IBOutlet weak var webView: WebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let urlString = "https://www.google.com/"
        self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)! as URL))

    }

    override var representedObject: Any? {
            didSet {
        }
    }
}
Zonily Jame
  • 5,053
  • 3
  • 30
  • 56
sonicsword
  • 21
  • 4
  • Shouldn't you just do `self.webView.loadRequest` instead of `self.webView.mainFrame.loadRequest`? – Zonily Jame Apr 16 '17 at 04:58
  • I get why this should work, but I am still getting an error: Value of type 'WebView' has no member 'loadRequest'. – sonicsword Apr 16 '17 at 05:09
  • See this for reference. although it's on objective c http://stackoverflow.com/questions/15921974/how-to-load-url-on-launch-in-a-webview-osx-project – Zonily Jame Apr 16 '17 at 05:31

1 Answers1

1

The API changed quite a bit in Swift 3. Here is the code that works:

import Cocoa
import WebKit

class ViewController: NSViewController {

    @IBOutlet weak var webView: WebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let urlString = "https://www.google.com/"
        let request = URLRequest(url: URL(string: urlString)!)
        self.webView.mainFrame.load(request)
    }
}

All the Foundation classes dropped the NS prefix (https://github.com/apple/swift-evolution/blob/master/proposals/0086-drop-foundation-ns.md) and because they changed the way parameters work, the load function changed quite a bit.

window

Papershine
  • 4,995
  • 2
  • 24
  • 48
  • THanks for this. This was exactly what I was looking for. It's surprising how much it changed, but now I see it i can understand it! :) – sonicsword Apr 16 '17 at 07:17