3

Is there any way to find the application identifier prefix in you iPhone application programmatically? Or even just to get the entire Application Identifier string?

I see you can find it by peeking into the "embedded.mobileprovision" file, but is there an easier way? (And I don't think that works if you're running in the simulator)

EDIT: Sorry, what I mean is the identifier PREFIX (10 characters). I've realized though that I don't actually need this, because the rest of the ID is guaranteed to be unique anyway.

MikeQ
  • 1,817
  • 3
  • 19
  • 27

3 Answers3

10

The bundle id is stored in the Info.plist of the app bundle as St3fan specified, but you should not grope at the Info.plist directly. Instead, use:

[[NSBundle mainBundle] bundleIdentifier]
orange
  • 454
  • 2
  • 3
4

You can programmatically retrieve the Bundle Seed ID by looking at the access group attribute (i.e. kSecAttrAccessGroup) of an existing KeyChain item. In the code below, I look up for an existing KeyChain entry and create one if it doesn't not exist. Once I have a KeyChain entry, I extract the access group information from it and return the access group's first component separated by "." (period) as the Bundle Seed ID.

+ (NSString *)bundleSeedID {
    NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
                           kSecClassGenericPassword, kSecClass,
                           @"bundleSeedID", kSecAttrAccount,
                           @"", kSecAttrService,
                           (id)kCFBooleanTrue, kSecReturnAttributes,
                           nil];
    CFDictionaryRef result = nil;
    OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result);
    if (status == errSecItemNotFound)
        status = SecItemAdd((CFDictionaryRef)query, (CFTypeRef *)&result);
    if (status != errSecSuccess)
        return nil;
    NSString *accessGroup = [(NSDictionary *)result objectForKey:kSecAttrAccessGroup];
    NSArray *components = [accessGroup componentsSeparatedByString:@"."];
    NSString *bundleSeedID = [[components objectEnumerator] nextObject];
    CFRelease(result);
    return bundleSeedID;
}
David H
  • 2,901
  • 1
  • 26
  • 21
  • Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead. http://stackoverflow.com/a/11841898/419 – Kev Aug 09 '12 at 22:44
  • Thanks for the heads up. Initially, I added an answer with just a link pointing to my other answer but SO automatically converts it to a comment to the question. I thought it would be more helpful to copy my answer here since all other answerers misinterpreted the question and gave the wrong answer. Should I delete this answer and link to the other answer as a comment to the question? – David H Aug 09 '12 at 23:00
  • Also, I checked out SO's "flag" feature and did not find a appropriate option to flag this question. The question has some other info that the other question lacks, so it's not an exact duplicate. – David H Aug 09 '12 at 23:10
0

If I understand correctly you are looking for bundle identifier? if you you can get this way.

NSString* appID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
Vladimir
  • 170,431
  • 36
  • 387
  • 313
rajendra
  • 9
  • 1
  • 2
    An anonymous user suggested this to be simpler: `NSString* appID = [[NSBundle mainBundle] bundleIdentifier];` – mplungjan Dec 19 '11 at 08:36