OSX Swift opening URL in default browser

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

I found a lot with UIApplication, for example

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

but it only works on iOS, not OSX

And Launch Services , I found there are no examples for fast and a lot outdated for OSX 10.10

Any help is appreciated - thanks.

+66
xcode swift nsurl macos openurl
Nov 02
source share
11 answers

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") } 
+118
Nov 03 '14 at 1:01
source share

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 must deploy NSURL.

+38
Apr 27 '15 at 19:34
source share

MacOS:

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

IOS:

 UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!) 
+12
Feb 17 '16 at 6:16
source share

When using Swift 3, you can open the 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 the url with Swift 3 by entering 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 this information helps anyone.

+11
Oct 18 '16 at 15:34
source share

Xcode update 9

 let url = URL(string: "https://www.google.com")! UIApplication.shared.open(url, options: [:], completionHandler: nil) 
+9
Jan 04 '18 at 20:08
source share

Just a bonus. If you want to open the URL in a specific browser (even for another client that can handle this 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, eg 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
Jan 02 '17 at 18:06
source share

For Swift 5 , Xcode 10, and MAC OS :

 NSWorkspace.shared.open(NSURL(string: "http://www.lichess.org")! as URL) 
+2
May 22 '19 at 3:09
source share

MacOS Xcode 10 Swift 4.2 Update

 NSWorkspace.shared.open(URL(string: "https://www.google.com")!) 
+1
Oct 31 '18 at 8:01
source share

Swift 4

  let url = NSURL(string: "https://google.com")! UIApplication.shared.openURL(url as URL) 
0
Dec 10 '18 at 23:17
source share

It is best to safely deploy optional variables:

 guard let url = URL(string: "https://www.google.com") else { return } NSWorkspace.shared.open(url) 
0
Apr 03 '19 at 21:03
source share

For iOS, it’s best to check all the necessary parameters.

 if let url = URL(string: "https://somurl.com/xyz"), UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(url) } } 
-one
Nov 30 '18 at 9:14
source share



All Articles