UIDevice uniqueIdentifier deprecated - What to do now?(不推荐使用 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 - 现在该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不推荐使用 UIDevice uniqueIdentifier - 现在该怎么办?
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01