如何在 iOS 上检索 UDID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4270200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I retrieve the UDID on iOS?
提问by Oh Danny Boy
I was wondering if it was possible to find out the UDID of the user's iPhone. Can someone shed some light?
我想知道是否有可能找出用户 iPhone 的 UDID。有人可以解释一下吗?
回答by Jacob Relkin
Note: Apple will no longer accept apps that access the UDID of a device starting May 1, 2013.
注意:自 2013 年 5 月 1 日起,Apple 将不再接受访问设备 UDID 的应用程序。
Instead, you must use the new methods identifierForVendor
and advertisingIdentifier
in iOS 6+ for accessing this. See related post herefor more detail.
相反,您必须使用新方法identifierForVendor
和advertisingIdentifier
iOS 6+ 来访问它。有关更多详细信息,请参阅此处的相关帖子。
Old way (deprecated and will result in App Store rejection)
旧方式(已弃用,将导致 App Store 拒绝)
NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
As of iOS7, the uniqueIdentifier
property is no longer available.
从 iOS7 开始,该uniqueIdentifier
属性不再可用。
See the UIDevice
reference.
请参阅UIDevice
参考资料。
回答by DZenBot
Like some said, in iOS5 this has been deprecated:
就像有人说的那样,在 iOS5 中这已被弃用:
NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
Apple now has 3 APIs to get a UUID(Universally Unique Identifier) of the device:
Apple 现在有 3 个 API 来获取设备的UUID(通用唯一标识符):
Alternative 1 (NSUUID class):
备选方案 1(NSUUID 类):
NSString *udid = [[NSUUID UUID] UUIDString];
Alternative 2 (UIDevice class):
备选方案 2(UIDevice 类):
NSString *udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Alternative 3 (ASIdentifierManager class, requieres AdSupport framework):
备选方案 3(ASIdentifierManager 类,需要 AdSupport 框架):
NSUUID *UUID = [[ASIdentifierManager sharedManager] advertisingIdentifier];
NSString *udid = [UUID UUIDString];
But they are all alphanumeric, and a tipical device unique identifier is only digits. So thos 3 APIs are useless, and the old [[UIDevice currentDevice] uniqueIdentifier] still works in iOS6, but for how long?
但它们都是字母数字,典型的设备唯一标识符只是数字。所以这 3 个 API 都没用了,旧的 [[UIDevice currentDevice] uniqueIdentifier] 在 iOS6 中仍然有效,但能持续多久呢?
If the "deprecated" warning bugs you, just add:
如果“已弃用”警告让您感到困扰,只需添加:
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Hope it helps!
希望能帮助到你!
回答by ravinder521986
In IOS5 and above, use this one to find UDID
在IOS5及以上,用这个找UDID
NSString *uuidString = nil;
CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid) {
uuidString = (__bridge NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}
NSLog(@"UDID: [%@]", uuidString);
回答by Miros
It might be worth mentioning that the identifierForVendor is not unique for a device, it can and will eventually change.
值得一提的是, identifierForVendor 不是设备唯一的,它可以并且最终会改变。
Apple says this about identifierForVendor:
苹果这样说identifierForVendor:
The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.
If the value is nil, wait and get the value again later. This happens, for example, after the device has been restarted but before the user has unlocked the device.
The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor's apps from the device and subsequently reinstalls one or more of them. The value can also change when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.
对于来自在同一设备上运行的同一供应商的应用程序,此属性的值是相同的。对于来自不同供应商的同一设备上的应用程序以及不同供应商的不同设备上的应用程序,将返回不同的值。
如果值为 nil,则等待稍后再次获取该值。例如,这种情况发生在设备重新启动之后但在用户解锁设备之前。
当应用程序(或来自同一供应商的另一个应用程序)安装在 iOS 设备上时,此属性中的值保持不变。当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值会发生变化。使用 Xcode 安装测试版本或使用临时分发在设备上安装应用程序时,该值也可能会更改。因此,如果您的应用程序将此属性的值存储在任何地方,您应该优雅地处理标识符更改的情况。
But what is even worse, is that it seems that Apple is also removing the last resort of using the MAC address as a Unique identifyer, according to this AVG Blog.
但更糟糕的是,根据这个 AVG 博客,Apple 似乎还取消了使用 MAC 地址作为唯一标识符的最后手段。
So what to do now, if we want to uniquely identify devices - eg. for licensing, settings or other purposes??
那么现在该怎么做,如果我们想唯一地识别设备 - 例如。用于许可、设置或其他目的??
回答by user3711806
Best way is to do it like this
最好的方法是这样做
NSString *udid = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
回答by El Tomato
In IOS5 and above, use this one to find UDID
NSString *uuidString = nil;
CFUUIDRef uuid = CFUUIDCreate(NULL); ...
在IOS5及以上,用这个找UDID
的NSString * uuidString =零;
CFUUIDRef uuid = CFUUIDCreate(NULL); ...
I think you are showing how to generate a unique identification number, not to get the unique id of a device.
我认为您正在展示如何生成唯一标识号,而不是获取设备的唯一 ID。
回答by AncAinu
I cannot comment, but I would like to warn others. In answer of DZenBot, all methods aren't giving the same results :
我无法发表评论,但我想警告其他人。在 DZenBot 的回答中,所有方法都没有给出相同的结果:
Alternative 1(NSUUID class):
备选方案 1(NSUUID 类):
NSString *udid = [[NSUUID UUID] UUIDString];
Alternative 2(UIDevice class) (Available in iOS 6.0 and later):
备选方案 2(UIDevice 类)(适用于 iOS 6.0 及更高版本):
NSString *udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Alternative 1generate a random UUIDwhile Alternative 2give a fixed UIDwhich won't change.
备选方案 1生成一个随机 UUID,而备选方案 2给出一个不会改变的固定 UID。
回答by swathy krishnan
It's very simple. Go to Xcode Window and select Devices or you can find it in Organizer.
这很简单。转到 Xcode 窗口并选择设备,或者您可以在管理器中找到它。
http://www.raywenderlich.com/2915/ios-code-signing-under-the-hood/organizerudid
http://www.raywenderlich.com/2915/ios-code-signing-under-the-hood/organizerudid
回答by Minh Hoang Truong
How about using [OpenUDID] lib https://github.com/ylechelle/OpenUDID
如何使用 [OpenUDID] lib https://github.com/ylechelle/OpenUDID
回答by Femina
Remember UDID is deprecated in IOS 5. In iOS 7, Apple now always returns a fixed value when querying the MAC to specifically thwart the MAC as base for an IDscheme. So you now really should use -[UIDevice identifierForVendor]or create a per-install UUID. Hope it will help for someone.
请记住,UDID 在 IOS 5 中已被弃用。在 iOS 7 中,Apple 现在在查询 MAC 时总是返回一个固定值,以专门阻止MAC 作为 ID方案的基础。所以你现在真的应该使用-[UIDevice identifierForVendor]或创建一个 per-install UUID。希望它会对某人有所帮助。