4

I've been trying to make a MenuItem in my macOS SwiftUI app to open a default browser to a specific URL.

Since I already had a MenuItem open up a PDF, I tried to modify this:

    @IBAction func Guide1(_ sender: Any) {
            if let pdfURL = Bundle.main.url(forResource: "Guide1", withExtension: "pdf"){
                if NSWorkspace.shared.open(pdfURL) {
            }
        }
    }

Into this:

    @IBAction func Google(_ sender: NSMenuItem) {
        if let fileURL = Bundle.main.url(forResource: "http://google.fi") {
                NSWorkspace.shared.open(fileURL as URL)
        }
    }

But kept being told that forResource should be replaced with forAuxiliaryExecutable. I make that change, and the code still does nothing.

I've mapped, of course the MenuItem Google to First Responder and then to the specific IBAction, but..

What am I missing?

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
esaruoho
  • 896
  • 1
  • 7
  • 25

1 Answers1

5

It should be as follows

@IBAction func Google(_ sender: NSMenuItem) {
    if let url = URL(string: "http://google.fi") {
        NSWorkspace.shared.open(url)
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690