Finding iPhone Application Identifier Prefix programmatically
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.
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]
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;
}
If I understand correctly you are looking for bundle identifier? if you you can get this way.
NSString* appID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
精彩评论