OSX Swift open URL in default browser

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")

}

For MacOS, you can use this:

let url = URL(string: "https://www.stackoverflow.com")!
NSWorkspace.sharedWorkspace().openURL(url))

For iOS, you can use the following:

let url = NSURL(string: "https://google.com")!
UIApplication.sharedApplication().openURL(url)

You have to unwrap NSURL.


macOS:

NSWorkspace.sharedWorkspace().openURL(NSURL(string: "https://google.com")!)

iOS:

UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!)