xcodemacosswiftnsurlopenurl

OSX Swift open URL in default browser


How to open a URL in the system default browser by using Swift as programming language and OSX as platform?

I found a lot with UIApplication like:

UIApplication.sharedApplication().openURL(NSURL(string: object.url))

but this works just on iOS and not on OSX

And the Launch Services, I found has no examples for swift and there is a lot deprecated for OSX 10.10.


Solution

  • Swift 3 or later

    import Cocoa
    
    let url = URL(string: "https://www.google.com")!
    if NSWorkspace.shared.open(url) {
        print("default browser was successfully opened")
    
    }