gpt4 book ai didi

swift - 即使我的默认浏览器不是在 macOS 中使用 Swift 的 Safari,如何在 Safari 中打开 url?

转载 作者:行者123 更新时间:2023-12-04 13:31:25 28 4
gpt4 key购买 nike

即使我的默认浏览器设置为 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)
或更换 method
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)
}

关于swift - 即使我的默认浏览器不是在 macOS 中使用 Swift 的 Safari,如何在 Safari 中打开 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64869975/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com