OSX Swift open URL in default browser

Multi tool use
Multi tool use


OSX Swift open URL in default browser



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.





I guess it's because we're supposed to use the new Extensions instead...
– Michele De Pascalis
Nov 2 '14 at 22:31





I upvoted because I neede to know how to do that in iOS only xd
– Josh
Nov 12 '15 at 11:37




6 Answers
6



Swift 3 or later


import Cocoa

if let url = URL(string: "https://www.google.com"),
NSWorkspace.shared().open(url) {
print("default browser was successfully opened")
}



For iOS, you can use the following:


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



You have to unwrap NSURL.





The question here is OS X, not iOS !!!
– Leo Dabus
Sep 3 '15 at 1:06





Why does this have 23 upvotes? Off topic to the question and should be removed.
– xandermonkey
Dec 10 '16 at 21:19





it works for iOS also. I upvoted.
– Lin
Sep 21 '17 at 0:13






This is misleading, as it does not work for the platform used in the question.
– KoCMoHaBTa
Nov 9 '17 at 21:20





Blame Google that indexed this page this way and all searches for iOS lead hare ... and it's useful :)
– Lachezar Todorov
Feb 12 at 16: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.





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 '17 at 13:14




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



macOS:


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



iOS:


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





Second answer is not for OSX...
– xandermonkey
Dec 10 '16 at 21:20





Good reference for earlier versions of Swift (I'm on 2.3)
– Stefano Buora
Aug 24 '17 at 15:54





Please describe that the second code block is for iOS
– Lachezar Todorov
Feb 12 at 16:11



xCode 9 update


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

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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

H,Ni,ewfLY222,iIjODazS H BQhH8a AQ43M0A2G
HJDTwaPNOkQqfvL J9avBmdXaaCgRBb4ECqstBG2bPulmSiYGdZqWqiZ7pg 40rI0LPtu,63USFl,6CLqIcL13b,f ywfO6Fj

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters