user973984
user973984

Reputation: 2824

Obtain Bundle Identifier programmatically

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

Upvotes: 240

Views: 85692

Answers (6)

Ash
Ash

Reputation: 5712

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

Objective-C:

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

Swift 3.0 :

let bundleIdentifier =  Bundle.main.bundleIdentifier

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

Upvotes: 1

peko
peko

Reputation: 11335

Objective-C

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

Swift 1.2

let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier

Swift 3.0

let bundleIdentifier = Bundle.main.bundleIdentifier

Xamarin.iOS

var bundleIdentifier = NSBundle.MainBundle.BundleIdentifier

Upvotes: 471

Tal Zion
Tal Zion

Reputation: 6526

To get the bundle identifier programmatically in Swift 3.0:

Swift 3.0

let bundle = Bundle.main.bundleIdentifier

Upvotes: 3

Tibidabo
Tibidabo

Reputation: 21571

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
}

Upvotes: 0

Alexander Kradenkov
Alexander Kradenkov

Reputation: 37

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

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

Upvotes: 3

DarkDust
DarkDust

Reputation: 92384

[[NSBundle mainBundle] bundleIdentifier];

(documentation)

Upvotes: 50

Related Questions