如何使用Cocoa获取具有给定ID的包的URL,反之亦然?

问题描述 投票:1回答:1

如果我有一个包标识符作为String,我如何找出它识别的包的URL?相反,如果我有一个捆绑的URL,我如何找出该捆绑的标识符为String

我想只使用Foundation / Cocoa API来做到这一点。

cocoa url foundation bundle-identifier app-bundle
1个回答
1
投票

您可以使用Bundleinit(identifier:),然后使用bundleURL从包标识符中获取URL,然后使用Bundleinit(url:),然后使用bundleIdentifier获取包的URL。

Example:

let myBundleUrl = Bundle(identifier: "com.me.app")?.bundleURL

let theirBundleId = Bundle(url: URL(fileURLWithPath: "/Applications/Mail.app"))?.bundleIdentifier

就像一张纸条,NSWorkspace还有一个API,用于从给定的包ID获取URL:urlForApplication(withBundleIdentifier:)。我不推荐这个,因为NSWorkspace没有用于从URL获取标识符的API。

© www.soinside.com 2019 - 2024. All rights reserved.