86

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
user2929462
  • 873
  • 1
  • 6
  • 5

8 Answers8

158

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

}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Technically this needs an "if let url = ...", since URL(string:) returns a URL?. Otherwise, great, thank you very much! – Apollo Grace Jan 12 '23 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 '23 at 17:26
56

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.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Connor Knabe
  • 897
  • 8
  • 11
  • 10
    Why does this have 23 upvotes? Off topic to the question and should be removed. – xandermonkey Dec 10 '16 at 21:19
  • 3
    This is misleading, as it does not work for the platform used in the question. – KoCMoHaBTa Nov 09 '17 at 21:20
  • 6
    Blame Google that indexed this page this way and all searches for iOS lead hare ... and it's useful :) – Lachezar Todorov Feb 12 '18 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 '18 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. – Peter Schorn Jul 02 '20 at 03:01
15

macOS:

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

iOS:

UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!)
pkamb
  • 33,281
  • 23
  • 160
  • 191
Alvin George
  • 14,148
  • 92
  • 64
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.

Dylan
  • 823
  • 2
  • 9
  • 33
  • 1
    As this is Swift 3, I don't think NSURL is necessary any more - just use URL: NSWorkspace.shared().open(URL(string: "https://google.com")!) and if let checkURL = URL(string: "https://google.com") { if NSWorkspace.shared().open(checkURL) { print("URL Successfully Opened") } } else { print("Invalid URL") } – gepree May 03 '17 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
  )
}
longkai
  • 3,598
  • 3
  • 22
  • 24
8

For Swift 5, Xcode 10 and MAC OS:

NSWorkspace.shared.open(NSURL(string: "http://www.lichess.org")! as URL)
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • 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 01 '19 at 15:23
7

xCode 9 update

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

UIApplication.shared.open(url, options: [:], completionHandler: nil)
gannonbarnett
  • 1,638
  • 1
  • 16
  • 28
4

MacOS Xcode 10 Swift 4.2 update

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