ios 以编程方式获取捆绑标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8883102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:21:39  来源:igfitidea点击:

Obtain Bundle Identifier programmatically

ioscocoa-touch

提问by user973984

How can I obtain a string of the Bundle Identifier programmatically from within my App?

如何从我的应用程序中以编程方式获取捆绑标识符字符串?

回答by peko

Objective-C

目标-C

NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

Swift 1.2

斯威夫特 1.2

let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier

Swift 3.0

斯威夫特 3.0

let bundleIdentifier = Bundle.main.bundleIdentifier

Xamarin.iOS

Xamarin.iOS

var bundleIdentifier = NSBundle.MainBundle.BundleIdentifier

回答by DarkDust

[[NSBundle mainBundle] bundleIdentifier];

(documentation)

(文档)

回答by Tal Zion

To get the bundle identifier programmatically in Swift 3.0:

Swift 3.0 中以编程方式获取包标识符:

Swift 3.0

斯威夫特 3.0

let bundle = Bundle.main.bundleIdentifier

回答by Alexander Kradenkov

You may need Core Foundation approach to get the value. ARC example is following:

您可能需要 Core Foundation 方法来获取值。ARC 示例如下:

NSString *value = (__bridge_transfer NSString *)CFDictionaryGetValue(CFBundleGetInfoDictionary(CFBundleGetMainBundle()),
                                                                     (const void *)(@"CFBundleIdentifier"));

回答by Aks

f you are trying to get it programmatically , you can use below line of code :

如果您试图以编程方式获取它,您可以使用以下代码行:

Objective-C:

目标-C:

NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

Swift 3.0 :

斯威夫特 3.0:

let bundleIdentifier =  Bundle.main.bundleIdentifier

Updated for latest swift It will work for both iOS and Mac apps.

更新为最新的 swift 它适用于 iOS 和 Mac 应用程序。

回答by Tibidabo

I use these macros to make it much shorter:

我使用这些宏使它更短:

#define BUNDLEID    [NSString stringWithString:[[NSBundle mainBundle] bundleIdentifier]]

#define BUNDLEIDEQUALS(bundleIdString) [BUNDLEID isEqualToString:bundleIdString]

so I can just compare like this:

所以我可以这样比较:

if (BUNDLEIDEQUALS(@"com.mycompany.myapp") {
    //do this
}