macOS 上的 Swift:使用 URL 作为参数打开 Chrome

Chr*_*ung 4 macos swift

我想使用 macOS 应用程序中的特定 URL 打开 Chrome。

我正在尝试这样的操作,但不幸的是它不起作用。Chrome 已打开,但未使用给定的 URL 字符串。

let configuration = NSWorkspace.OpenConfiguration()
configuration.arguments = [url.absoluteString]
let appUrl = URL(fileURLWithPath: "/Applications/Google Chrome.app")
NSWorkspace.shared.openApplication(at: appUrl,
    configuration: configuration,
    completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

open()在这种情况下对我没有帮助,因为我不想打开标准浏览器。

非常感谢帮助!

Chr*_*ung 5

我解决了。

NSWorkspace.shared.open([url],
    withApplicationAt: URL(fileURLWithPath: "/Applications/Google Chrome.app"),
    configuration: configuration)
Run Code Online (Sandbox Code Playgroud)

成功了

  • 您应该使用 urlForApplication(withBundleIdentifier:) 来获取 URL,而不是对其进行硬编码。与用户可以更改的名称或路径相比,捆绑包 ID 是一种更安全的识别应用程序的方法。 (3认同)