OSX Swift open URL in default browser

XcodeMacosSwiftNsurlOpenurl

Xcode Problem Overview


How to open a url in system default browser by using Swift as programming language and OSX as plattform.

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

Any help welcome - thanks.

Xcode Solutions


Solution 1 - Xcode

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

Solution 2 - Xcode

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.

Solution 3 - Xcode

macOS:

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

iOS:

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

Solution 4 - Xcode

When using Swift 3, you can open a webpage in the default browser using the following:

NSWorkspace.shared().open(NSURL(string: "https://google.com")! as URL)

In the accepted answer above, you can also check a URL using Swift 3 by inputting the following:

if let checkURL = NSURL(string: "https://google.com") {
    if NSWorkspace.shared().open(checkURL as URL) {
        print("URL Successfully Opened")
    }
} else {
    print("Invalid URL")
}

I hope that this information helps whomever it applies to.

Solution 5 - Xcode

Just a bonus. If you want to open a URL in a specific browser(even other client who can handle that URL), here is the Swift 3 code tested on Xcode 8.2.1 and macOS 10.12.2.

/// appId: `nil` use the default HTTP client, or set what you want, e.g. Safari `com.apple.Safari`
func open(url: URL, appId: String? = nil) -> Bool {
  return NSWorkspace.shared().open(
    [url],
    withAppBundleIdentifier: appId,
    options: NSWorkspaceLaunchOptions.default,
    additionalEventParamDescriptor: nil,
    launchIdentifiers: nil
  )
}

Solution 6 - Xcode

xCode 9 update

let url = URL(string: "https://www.google.com")!

UIApplication.shared.open(url, options: [:], completionHandler: nil)

Solution 7 - Xcode

For Swift 5, Xcode 10 and MAC OS:

NSWorkspace.shared.open(NSURL(string: "http://www.lichess.org")! as URL)

Solution 8 - Xcode

MacOS Xcode 10 Swift 4.2 update

NSWorkspace.shared.open(URL(string: "https://www.google.com")!)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2929462View Question on Stackoverflow
Solution 1 - XcodeLeo DabusView Answer on Stackoverflow
Solution 2 - XcodeConnor KnabeView Answer on Stackoverflow
Solution 3 - XcodeAlvin GeorgeView Answer on Stackoverflow
Solution 4 - XcodeDylanView Answer on Stackoverflow
Solution 5 - XcodelongkaiView Answer on Stackoverflow
Solution 6 - XcodeganskiView Answer on Stackoverflow
Solution 7 - XcodeHCarraskoView Answer on Stackoverflow
Solution 8 - XcodeXavier LasneView Answer on Stackoverflow