请务必注意 UDID 和 UUID 之间的区别。
UDID“唯一设备 ID”是特定于硬件的。对于特定设备,它永远不会改变。出于这个原因,它已经成为一个隐私问题,Apple 正在阻止尝试使用它的应用程序。因此,Apple 生成了一个可选择退出的“设备 ID”哈希,特别是用于广告使用。这个新的 ID 哈希称为 IFA,在 iOS 6.0+ 中可用。
UUID“通用唯一ID”不是硬件特定的。它是用于标识设备的哈希值;但不是一个绝对值。例如PhoneGap根据设备属性生成UUID;这就是您执行 device.uuid 时得到的结果。如果您删除应用程序并重新安装,您将获得一个新的 id 哈希。UUID 没有被 Apple 阻止。
我认为在您的情况下最好的解决方案是使用 IFA,并将 OpenUDID 作为 iOS < 6.0 的备份。
这是我们使用的代码。如果 IFA 不可用,请获取 OpenUDID。您必须安装 OpenUDID,在此处阅读更多相关信息,https://github.com/ylechelle/OpenUDID。
NSString* uuid = nil;
if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
// IOS 6 new Unique Identifier implementation, IFA
uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
// Before iOS6 (or if IFA disabled) you shoud use a custom implementation for uuid
// Here I use OpenUDID (you have to import it into your project)
// https://github.com/ylechelle/OpenUDID
NSString* openUDID = [OpenUDID value];
uuid = [OpenUDID value];
}