如何在Swift中以编程方式获取应用程序ID?

How to obtain the app ID programmatically in Swift?

在 macOS 应用程序中,我使用此代码在 Application Support 文件夹中创建一个目录。

let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")

如何在Swift中以编程方式获取字符串com.myCompany.myApp

我看到了这个问题,但我不确定如何在我的 macOS Swift 应用程序中使用它:Access App Identifier Prefix programmatically

if let bundleIdentifier = Bundle.main.bundleIdentifier {
    appSupportURL.appendingPathComponent("\(bundleIdentifier)").appendingPathComponent("Documents")
}

一点解释:属性 bundleIdentifier 是可选的,因此您必须安全地解包该值,这样就不会要求您输入任何感叹号:)

获取应用程序 ID 非常简单:

let bundleIdentifier =  Bundle.main.bundleIdentifier
  appSupportURL.appendingPathComponent("\(bundleIdentifier)").appendingPathComponent("Documents")

包标识符是分配给包的 Info.plist 文件中的 CFBundleIdentifier 键的字符串。此字符串通常使用反向 DNS 表示法进行格式化,以防止名称 space 与其他公司的开发人员发生冲突。例如,Apple 的 Finder 插件可能使用字符串 com.apple.Finder.MyGetInfoPlugin 作为其包标识符。不需要在代码周围传递指向包对象的指针,需要引用包的客户端可以简单地使用包标识符来检索它

更多详情及其他操作详情,请查看

https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html