如何从设备生成唯一标识符?

3

我想知道在苹果设备(iPod Touch,iPhone或iPad)上可以使用和存储什么来生成唯一标识符,例如IMEI或其他内容... 我只需要一个唯一的东西,以便我的应用程序可以执行“同一设备一次”的验证。

谢谢。


那个可能的重复问题的答案已经过时了... - Ben Flynn
4个回答

7

您可以使用NSUUID。但是这个值会改变,因此您可以在应用程序首次打开时调用它并保存它来实现该功能。

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
        //App has previously launched

}
else
{
    //First launch
    NSString *identifierString = [[NSUUID UUID] UUIDString];
    [[NSUserDefaults standardUserDefaults] setObject:identifierString forKey:@"uuidKey"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

编辑:如需更多信息和可用选项,请参阅这篇优秀文章


哇...你很快,而且还附带了示例代码,兄弟...非常感谢! - Saint Robson

2

如果您使用的是iOS 6.0或更高版本,您可以使用UIDevice中的identifierForVendor方法。

NSUUID*   pUUID;
UIDevice* pThisDvc;

pThisDvc = [UIDevice currentDevice];
if ( pThisDvc )
{
    pUUID = [pThisDvc identifierForVendor];
}

为什么我们需要检查 if (pThisDvc) ?? 是否有任何情况 [UIDevice currentDevice] 不返回有效对象? - n00bProgrammer

1
NSUUID* identifier = [[UIDevice currentDevice] identifierForVendor];

NSString* uniqueIdentifier = [identifier UUIDString];

这将返回当前设备的唯一标识符,对于每个应用程序都是唯一的。苹果建议在一般情况下使用此标识符,在广告目的下使用advertisingIdentifier

在实现广告服务系统时,请使用ASIdentifierManager类中的advertisingIdentifier属性值,而不是使用此属性。使用该属性需要遵循类讨论中为正确使用该标识符设置的指南。有关更多信息,请参见ASIdentifierManager Class Reference。

...来自这里



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