How to open a url in safari even if my default browser is not Safari using Swift in macOS?
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)
}
How to open a url in Safari instead of the default browser (JavaScript)?
I managed to do this using Swift. I added the following webkit delegate method.
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
NSWorkspace.shared.open([navigationAction.request.url!], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
return nil
}
Can't open a link when the default browser has been changed
For iOS 14+
I suggest to add this to your Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>https</string>
</array>
By adding this you can continue to use method canOpenURL(_ url: URL) -> Bool
guard let url = URL(string: "https://example.com") else { return }
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
Open non-URL string in Safari (e.g. Search)
You can use x-web-search://
scheme with x-web-search://?[SearchValue]
.
Sample code:
let urlStr = "x-web-search://?[SearchValue]"
if let url = URL(string: urlStr) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { didEnd in
print("Did End: \(didEnd)")
}
} else {
print("Can't open URL")
}
} else {
print("Isn't URL")
}
Related Topics
Why Does Classa Adopting Protocolb Not Satisfy the Protocolb Requirement
Nsurl Fail Able Initialiser Initwithstring: Does Not Return Nil on Empty String in Swift
Swift Why Isn't My Date Object That's (Equatable) Equal After Converting It to a String and Back
Change Mkmarkerannotationview Size
Swift: Nil Error When Using Self.Moc.Save() to Save in Core Data
Getch() Equivalent in Swift: Read a Single Character from Stdin Without a Newline
Using Tvposterimage in Tvuikit for Tvos 12
Swiftui: Unexpected Animation When Using a Non @State Var
Realitykit - How to Add Motion to a Loaded Modelentity from Usdz File
How to Create Lazy Combinations
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Stanford Calculator App Keeps Crashing
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
How to Move a Model and Generate Its Collision Shape at the Same Time