如您所知,Apple 最近弃用了 UDID。所以我对此的解决方案是
- 生成 CFUUID
- 将其保存到钥匙串
- 之后重新访问钥匙串项目。
这一直运作良好。但是,由于某种原因,我们最近看到,随着企业版本的安装,我们得到了不同的 UUID(它应该使用我们唯一的访问密钥存储在钥匙串上)。
有没有人遇到过这种情况?这是创建 UUID 并将其存储到钥匙串上的代码。
+ (NSString *)registerUUIDWithKeyChain
{
CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *uuidString = (NSString *) CFUUIDCreateString(NULL, udid);
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UniqueApp" accessGroup:nil];
NSString *userName = @"UniqueAppName";
NSString *password = uuidString;
[keychainItem setObject:userName forKey:(id)kSecAttrAccount];
[keychainItem setObject:password forKey:(id)kSecValueData];
[keychainItem release];
return uuidString;
}
+ (NSString *)userUUID
{
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UniqueApp" accessGroup:nil];
//Accesing the v_data was the only way. For some reason there is a runtime issue if we try to access it though "kSecValueData"
NSString *uuid = [keychainItem.keychainItemData objectForKey:@"v_Data"];
//Check if the app is installed for the first time on the device. If YES register the UUID in to the keychain.
//Also check if it is a reinstall by accessing the previous keyChainItem with our Identifier.
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"firstRun"] intValue] == 0 && !(uuid.length > 0))
{
uuid = [UIDevice_Additions registerUUIDWithKeyChain];
NSLog(@"\n First Time Registered UUID is %@", uuid);
//after stuff done
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:1] forKey:@"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
[keychainItem release];
return uuid;
}
[keychainItem release];
return uuid;
}
@end