Obtain Bundle Identifier programmatically

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


Solution 1:

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:

[[NSBundle mainBundle] bundleIdentifier];

(documentation)

Solution 3:

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:

To get the bundle identifier programmatically in Swift 3.0:

Swift 3.0

let bundle = Bundle.main.bundleIdentifier

Solution 5:

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.