Obtain Bundle Identifier programmatically

前端 未结 6 2005
日久生厌
日久生厌 2020-12-12 13:50

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

相关标签:
6条回答
  • 2020-12-12 14:27

    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"));
    
    0 讨论(0)
  • 2020-12-12 14:28

    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
    
    0 讨论(0)
  • 2020-12-12 14:34

    To get the bundle identifier programmatically in Swift 3.0:

    Swift 3.0

    let bundle = Bundle.main.bundleIdentifier
    
    0 讨论(0)
  • 2020-12-12 14:37
    [[NSBundle mainBundle] bundleIdentifier];
    

    (documentation)

    0 讨论(0)
  • 2020-12-12 14:37

    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.

    0 讨论(0)
  • 2020-12-12 14:52

    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
    }
    
    0 讨论(0)
提交回复
热议问题