5

如您所知,Apple 最近弃用了 UDID。所以我对此的解决方案是

  1. 生成 CFUUID
  2. 将其保存到钥匙串
  3. 之后重新访问钥匙串项目。

这一直运作良好。但是,由于某种原因,我们最近看到,随着企业版本的安装,我们得到了不同的 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
4

2 回答 2

3

好的,

在与这个问题斗争了一天之后,我找到了触发这个问题的原因。

  • 钥匙串取决于证书
  • 使用不同的证书创建企业构建
  • 因此,当代码尝试从企业构建访问您的密钥时,您将找不到它,因此生成的代码将生成一个新密钥。
  • 解决方案是创建您的钥匙串,以便全球访问。您可以在 KeyChainWrapper 初始化方法中更改 accessGroup 变量。

祝你好运!

于 2012-04-27T01:19:54.660 回答
-3

您可以使用 UUID 的 MAC 地址

- (NSString *) macaddress
{
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;

    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;

    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error\n");
        return NULL;
    }

    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1\n");
        return NULL;
    }

    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!\n");
        return NULL;
    }

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }

    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);

    return outstring;
}
于 2012-04-26T00:49:19.660 回答