UIDevice的uniqueIdentifier已经被弃用 - 现在该怎么做?

511
刚刚得知,在iOS 5中已将UIDevice唯一标识符属性(uniqueIdentifier property)作废,在iOS 7及以上版本中也不再提供该属性的支持。目前似乎没有可用或即将推出的替代方法或属性。

我们许多现有的应用程序都高度依赖此属性来唯一标识特定设备。那么我们该如何解决这个问题呢?

2011-2012年的文档建议:

特别注意事项

不要使用uniqueIdentifier属性。要创建与您的应用程序特定的唯一标识符,您可以调用CFUUIDCreate函数创建UUID,并使用NSUserDefaults类将其写入默认数据库。

但是如果用户卸载并重新安装该应用程序,该值将不同。


1
对于仍在使用uniqueIdentifier的应用程序,iOS7现在返回FFFFFFFF + identifierForVendor,这破坏了许多编写不良的非续订订阅应用程序。 - Rhythmic Fistman
如果你的应用程序幸运地使用了推送通知,你可以使用从苹果推送服务返回的令牌,它对于每个设备都是唯一的。 - Calin Chitu
如果用户不接受推送通知,您是否仍会为该用户获取推送ID? - Chase Roberts
32个回答

-1
请勿使用这些库 - libOmnitureAppMeasurement,它使用了uniqueIdentifier,而苹果已不再支持。

-2

给你一个小技巧:

/**
 @method uniqueDeviceIdentifier
 @abstract A unique device identifier is a hash value composed from various hardware identifiers such
 as the device’s serial number. It is guaranteed to be unique for every device but cannot 
 be tied to a user account. [UIDevice Class Reference]
 @return An 1-way hashed identifier unique to this device.
 */
+ (NSString *)uniqueDeviceIdentifier {      
    NSString *systemId = nil;
    // We collect it as long as it is available along with a randomly generated ID.
    // This way, when this becomes unavailable we can map existing users so the
    // new vs returning counts do not break.
    if (([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0f)) {
        SEL udidSelector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:udidSelector]) {
            systemId = [[UIDevice currentDevice] performSelector:udidSelector];
        }
    }
    else {
        systemId = [NSUUID UUID];
    }
    return systemId;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接