即使我的默认浏览器设置为 Google Chrome,我也试图在 Safari 中打开一个 url。我怎么做?我正在使用以下代码,但它仅在默认浏览器中打开 url。
let requiredURL = URL(string: "https://www.google.com)")!
NSWorkspace.shared.open(requiredURL)
我是否必须使用以下功能:NSWorkspace.shared.open(urls: [URL], withApplicationAt: URL, options: NSWorkspace.LaunchOptions, configuration: [NSWorkspace.LaunchConfigurationKey : Any])
如果是这样,我该如何实现?
最佳答案
您可以使用 NSWorkspace open method在 macOS 11 中已弃用
let url = URL(string: "https://www.google.com")!
NSWorkspace.shared.open([url], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
或更换methoddo {
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)
}
关于swift - 即使我的默认浏览器不是在 macOS 中使用 Swift 的 Safari,如何在 Safari 中打开 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64869975/