238

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

ehehhh
  • 1,066
  • 3
  • 16
  • 27
user973984
  • 2,804
  • 2
  • 15
  • 13

6 Answers6

468

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
bucyDev
  • 142
  • 8
peko
  • 11,267
  • 4
  • 33
  • 48
  • 10
    This answer is not limited to iOS. It works for Mac apps too. – Jonny Oct 31 '13 at 01:40
  • 9
    In Swift, use `let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier` – Tim Arnold Nov 13 '14 at 17:11
  • 1
    (you can delete this comment) but I love the feeling of reading the answer then in the comment, see something like @Jonny s and Tim (although you can see this in another whole answer), it links to something else that still relevant and useful. Thanks for great community guys. – haxpor Mar 02 '15 at 00:47
  • 2
    Swift3: `Bundle.main.bundleIdentifier!` – Sebastian Roth Sep 14 '16 at 07:05
50
[[NSBundle mainBundle] bundleIdentifier];

(documentation)

DarkDust
  • 90,870
  • 19
  • 190
  • 224
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"));
3

To get the bundle identifier programmatically in Swift 3.0:

Swift 3.0

let bundle = Bundle.main.bundleIdentifier
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
1

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.

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Ash
  • 5,525
  • 1
  • 40
  • 34
0

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
}
Tibidabo
  • 21,461
  • 5
  • 90
  • 86