UIDevice uniqueIdentifier 已弃用 - 现在该怎么办? [英] UIDevice uniqueIdentifier deprecated - What to do now?

查看:29
本文介绍了UIDevice uniqueIdentifier 已弃用 - 现在该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚曝光UIDevice uniqueIdentifier 属性在 iOS 5 中已弃用,在 iOS 7 及更高版本中不可用.似乎没有可用或即将出现的替代方法或属性.

It has just come to light that the UIDevice uniqueIdentifier property is deprecated in iOS 5 and unavailable in iOS 7 and above. No alternative method or property appears to be available or forthcoming.

我们现有的许多应用都严格依赖此属性来唯一标识特定设备.今后我们将如何处理这个问题?

Many of our existing apps are tightly dependent on this property for uniquely identifying a particular device. How might we handle this problem going forward?

来自 2011-2012 年的文档是:

特殊注意事项

不要使用 uniqueIdentifier 属性.创建特定的唯一标识符到您的应用程序,您可以调用CFUUIDCreate 函数来创建一个UUID,然后编写使用 NSUserDefaults 类将其添加到默认数据库.

Do not use the uniqueIdentifier property. To create a unique identifier specific to your app, you can call the CFUUIDCreate function to create a UUID, and write it to the defaults database using the NSUserDefaults class.

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

However this value won't be the same if a user uninstalls and re-installs the app.

推荐答案

CFUUIDCreate 创建的 UUID 是唯一的,如果用户卸载并重新安装应用程序:你每次都会得到一个新的.

A UUID created by CFUUIDCreate is unique if a user uninstalls and re-installs the app: you will get a new one each time.

但是你可能希望它不是唯一的,我.e.当用户卸载并重新安装应用程序时,它应该保持不变.这需要一些努力,因为最可靠的每设备标识符似乎是 MAC 地址.您可以查询MAC和将其用作 UUID.

But you might want it to be not unique, i. e. it should stay the same when the user uninstalls and re-installs the app. This requires a bit of effort, since the most reliable per-device-identifier seems to be the MAC address. You could query the MAC and use that as UUID.

当然需要始终查询同一接口的 MAC.我想最好的选择是 en0.MAC 始终存在,即使接口没有 IP/已关闭.

One needs to always query the MAC of the same interface, of course. I guess the best bet is with en0. The MAC is always present, even if the interface has no IP/is down.

编辑 2: 正如其他人所指出的,自 iOS 6 以来首选的解决方案是 -[UIDevice identifierForVendor].在大多数情况下,您应该可以将其用作旧 -[UIDevice uniqueIdentifier] 的替代品(但在应用程序首次启动时创建的 UUID 是 Apple 的样子想要你使用).

Edit 2: As was pointed out by others, the preferred solution since iOS 6 is -[UIDevice identifierForVendor]. In most cases, you should be able use it as a drop-in replacement to the old -[UIDevice uniqueIdentifier] (but a UUID that is created when the app starts for the first time is what Apple seems to want you to use).

编辑 3: 所以这个要点不会在评论噪音中迷失:不要使用 MAC 作为 UUID,创建一个 使用 MAC 哈希.即使在重新安装和应用程序之间(如果散列以相同的方式完成),该散列每次将始终创建相同的结果.无论如何,如今(2013 年)这不再需要,除非您需要 iOS 上的稳定"设备标识符 <6.0.

Edit 3: So this major point doesn't get lost in the comment noise: do not use the MAC as UUID, create a hash using the MAC. That hash will always create the same result every time, even across reinstalls and apps (if the hashing is done in the same way). Anyways, nowadays (2013) this isn't necessary any more except if you need a "stable" device identifier on iOS < 6.0.

编辑 4:在 iOS 7 中,Apple 现在在查询 MAC 时总是返回一个固定值,以明确阻止MAC 作为 ID 方案的基础.所以你现在真的应该使用 -[UIDevice identifierForVendor] 或创建一个每个安装的 UUID.

Edit 4: In iOS 7, Apple now always returns a fixed value when querying the MAC to specifically thwart the MAC as base for an ID scheme. So you now really should use -[UIDevice identifierForVendor] or create a per-install UUID.

这篇关于UIDevice uniqueIdentifier 已弃用 - 现在该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆