89

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.

1
  • 1
    I guess it's because we're supposed to use the new Extensions instead... Nov 2, 2014 at 22:31

8 Answers 8

161

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

}
2
  • Technically this needs an "if let url = ...", since URL(string:) returns a URL?. Otherwise, great, thank you very much! Jan 12, 2023 at 13:06
  • @ApolloGrace well if you know the string will never mutate and it is a valid url you can safely force unwrap the initializer result URL(string: "https://www.google.com")! therefore in this case url is not optional. url type is URL
    – Leo Dabus
    Jan 12, 2023 at 17:26
59

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.

5
  • 10
    Why does this have 23 upvotes? Off topic to the question and should be removed. Dec 10, 2016 at 21:19
  • 3
    This is misleading, as it does not work for the platform used in the question.
    – KoCMoHaBTa
    Nov 9, 2017 at 21:20
  • 6
    Blame Google that indexed this page this way and all searches for iOS lead hare ... and it's useful :) Feb 12, 2018 at 16:11
  • If the URL is nil because the string couldn't be a valid URL, this code crashes. You need to be careful in Swift when you're using exclamation points, or the whole program crashes.
    – Kaydell
    Feb 21, 2018 at 23:12
  • 1
    @Kaydell If you're using a string literal or any compile-time constant to create the url, then force-unwrapping it is perfectly fine. In these cases, there's no point in adding any error-handling code; if the value is nil, then that just indicates you made a typo. Jul 2, 2020 at 3:01
15

macOS:

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

iOS:

UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!)
2
  • Good reference for earlier versions of Swift (I'm on 2.3) Aug 24, 2017 at 15:54
  • 2
    Please describe that the second code block is for iOS Feb 12, 2018 at 16:11
11

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.

1
  • 2
    As this is Swift 3, I don't think NSURL is necessary any more - just use URL: NSWorkspace.shared().open(URL(string: "google.com")!) and if let checkURL = URL(string: "google.com") { if NSWorkspace.shared().open(checkURL) { print("URL Successfully Opened") } } else { print("Invalid URL") }
    – gepree
    May 3, 2017 at 13:14
8

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

For Swift 5, Xcode 10 and MAC OS:

NSWorkspace.shared.open(NSURL(string: "http://www.lichess.org")! as URL)
2
  • 4
    Just use URL, not both URL and NSURL. For example: if let url = URL(string: "https://www.lichess.org") { NSWorkspace.shared.open(url) }
    – gepree
    Aug 1, 2019 at 15:23
  • Why would you use NSURL and then cast it to URL in Swift 5? Why not directly create a URL?
    – HangarRash
    Sep 10, 2023 at 16:47
6

xCode 9 update

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

UIApplication.shared.open(url, options: [:], completionHandler: nil)
1
  • 6
    This is for iOS and not macOS
    – Grumme
    Aug 3, 2018 at 13:26
4

MacOS Xcode 10 Swift 4.2 update

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.