ios 以编程方式获取IOS设备的UDID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31652359/
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
get UDID of IOS device programmatically?
提问by Nishi Bansal
I want to get UDID of iOS device programmatically. I am using the following code to get UDID of iOS device.
我想以编程方式获取 iOS 设备的 UDID。我正在使用以下代码来获取 iOS 设备的 UDID。
NSUUID *uuid = [NSUUID UUID];
NSString *uuidString = uuid.UUIDString;
But output I get is different from actual UDID of my device.
但是我得到的输出与我设备的实际 UDID 不同。
回答by Anbu.Karthik
NSString* identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
NSLog(@"output is : %@", identifier);
Swift 2.X (HERE X DENOTE ABOVE ALL VERSION FROM 2.0)
Swift 2.X(此处 X 表示 2.0 中的所有版本)
let identifier: String = UIDevice.currentDevice().identifierForVendor().UUIDString()
NSLog("output is : %@", identifier)
Swift3
斯威夫特3
let identifier = UIDevice.current.identifierForVendor?.uuidString
NSLog("output is : %@", identifier! as String)
additional reference
额外参考
Apple is apparently starting to remove access to the UDID (Unique Device IDentifier) in iOS5. In any event, the best you can now do for identification purposes is to use a UUID (Universally Unique IDentifier). This has to be on a per-app basis. That is, there is no way to identify the device any longer, but you can identify an app on a device.As long as the user doesn't completely delete the app, then this identifier will persist between app launches, and at least let you identify the same user using a particular app on a device. Unfortunately, if the user completely deletes and then reinstalls the app then the ID will change, but this is the best anyone can do going forward.
苹果显然开始在 iOS5 中移除对 UDID(唯一设备标识符)的访问。无论如何,您现在可以为识别目的做的最好的事情是使用 UUID(通用唯一标识符)。这必须基于每个应用程序。也就是说,没有办法再识别设备了,但是你可以识别设备上的应用程序。只要用户没有完全删除应用程序,那么这个标识符将在应用程序启动之间持续存在,至少让您在设备上使用特定应用程序识别同一用户。不幸的是,如果用户完全删除然后重新安装应用程序,那么 ID 将会改变,但这是任何人都可以做的最好的事情。