Reputation: 173
I am trying to open a url in Safari even if my default browser is set to Google Chrome. How do I do that? I am using the following code, but it opens the url in the default browser only.
let requiredURL = URL(string: "https://www.google.com)")!
NSWorkspace.shared.open(requiredURL)
Do I have to use the following function:
NSWorkspace.shared.open(urls: [URL], withApplicationAt: URL, options: NSWorkspace.LaunchOptions, configuration: [NSWorkspace.LaunchConfigurationKey : Any])
If so, how do I implement it?
Upvotes: 5
Views: 1850
Reputation: 236360
You can use NSWorkspace open method which has been deprecated in macOS 11
let url = URL(string: "https://www.google.com")!
NSWorkspace.shared.open([url], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
Or the replacement method (macOS 10.15 or later)
do {
let safariURL = try FileManager.default.url(for: .applicationDirectory, in: .localDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Safari.app")
NSWorkspace.shared.open([url], withApplicationAt: safariURL, configuration: .init()) { (runningApp, error) in
print("running app", runningApp ?? "nil")
}
} catch {
print(error)
}
Upvotes: 1