且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS唯一用户标识符

更新时间:2023-02-07 13:57:26

首先,UDID是仅弃用在iOS 5中。这并不意味着它已经消失了。

Firstly, the UDID is only deprecated in iOS 5. That doesn't mean it's gone (yet).

其次,你应该问自己是否真的需要这样的东西。如果用户获得新设备并在其上安装您的应用程序该怎么办?相同的用户,但UDID已更改。同时,原始用户可能已经售出了他的旧设备,所以现在一个全新的用户安装你的应用程序,你认为它是基于UDID的不同的人。

Secondly, you should ask yourself if you really need such a thing. What if the user gets a new device and installs your app on that? Same user, but the UDID has changed. Meanwhile, the original user might have sold his old device so now a completely new user installs your app and you think it's a different person based on the UDID.

如果你不喜欢不需要UDID,使用 CFUUIDCreate()来创建一个唯一的ID,并在首次启动时将其安全保存到用户默认值(使用 CFUUIDCreateString()首先将UUID转换为字符串)。它将在备份和恢复后继续存在,甚至在原始用户切换到新设备时也会出现。它在很多方面都是UDID的更好选择。

If you don't need the UDID, use CFUUIDCreate() to create a unique ID and safe it to the user defaults on first launch (use CFUUIDCreateString() to convert the UUID to a string first). It will survive backups and restores and even come along with the original user when they switch to a new device. It's in many ways a better option that the UDID.

如果你真的需要一个独特的设备标识符(听起来不像你做的那样) ),按照Suhail的回答中指出的MAC地址。

If you really need a unique device identifier (it doesn't sound like you do), go for the MAC address as pointed out in Suhail's answer.