Obtain Bundle Identifier programmatically

IosCocoa Touch

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

[[NSBundle mainBundle] bundleIdentifier];

(documentation)

Solution 3 - Ios

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"));

Solution 4 - Ios

To get the bundle identifier programmatically in Swift 3.0:

> Swift 3.0

let bundle = Bundle.main.bundleIdentifier

Solution 5 - Ios

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.

Solution 6 - Ios

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser973984View Question on Stackoverflow
Solution 1 - IospekoView Answer on Stackoverflow
Solution 2 - IosDarkDustView Answer on Stackoverflow
Solution 3 - IosAlexander KradenkovView Answer on Stackoverflow
Solution 4 - IosTal ZionView Answer on Stackoverflow
Solution 5 - IosAshView Answer on Stackoverflow
Solution 6 - IosTibidaboView Answer on Stackoverflow