首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Cocoa中的应用程序名称中获取Bundle Identifier?

如何从Cocoa中的应用程序名称中获取Bundle Identifier?
EN

Stack Overflow用户
提问于 2012-02-23 14:38:06
回答 3查看 7.1K关注 0票数 7

假设您有一个应用程序的名称Mail.app,如何以编程方式从该应用程序名称中获取com.apple.mail

EN

回答 3

Stack Overflow用户

发布于 2018-01-12 01:36:15

在Swift 4,macOS 10.13.2中扩展Francesco Germinara的答案:

代码语言:javascript
复制
extension Bundle {
    class func bundleIDFor(appNamed appName: String) -> String? {
        if let appPath = NSWorkspace.shared.fullPath(forApplication: appName) {
            if let itsBundle = Bundle(path: appPath) { // < in my build this condition fails if we're looking for the ID of the app we're running...
                if let itsID = itsBundle.bundleIdentifier {
                    return itsID
                }
            } else {
                //Attempt to get the current running app.
                //This is probably too simplistic a catch for every single possibility
                if let ownID =  Bundle.main.bundleIdentifier {
                    return ownID
                }
            }
        }
        return nil
    }
}

把它放到你的Swift项目中,你可以这样叫它:

代码语言:javascript
复制
let id = Bundle.bundleIDFor(appNamed: "Mail.app")

代码语言:javascript
复制
let id = Bundle.bundleIDFor(appNamed: "Mail")
票数 1
EN

Stack Overflow用户

发布于 2012-02-23 14:42:32

它是Contents/Info.plist中关键字CFBundleIdentifier的值

票数 0
EN

Stack Overflow用户

发布于 2014-06-26 05:29:03

这是一种可能的快速实现。

代码语言:javascript
复制
func bundleIdentifierForApplicationName(appName : String) -> String
{
    var workspace = NSWorkspace.sharedWorkspace()
    var appPath : String = workspace.fullPathForApplication(appName)
    if (appPath != "") {
        var appBundle : NSBundle = NSBundle(path:appPath)
     return appBundle.bundleIdentifier
    }
   return ""
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9408293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档