如何在 Swift 中获取唯一的设备 ID? [英] How to get a unique device ID in Swift?
问题描述
如何在 Swift 中获取设备的唯一 ID?
我需要一个 ID 以在数据库中使用,并作为我社交应用中网络服务的 API 密钥.用于跟踪此设备的日常使用情况并将其查询限制在数据库中.
你可以使用这个(Swift 3):
UIDevice.current.identifierForVendor!.uuidString
对于旧版本:
UIDevice.currentDevice().identifierForVendor
或者如果你想要一个字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString
在用户卸载应用程序后,不再有唯一标识设备的方法.文档说:
当应用程序(或来自同一供应商的其他应用程序)安装在 iOS 设备上时,此属性中的值保持不变.当用户从设备中删除该供应商的所有应用并随后重新安装其中一个或多个应用时,该值会发生变化.
您可能还想阅读 Mattt Thompson 的这篇文章以了解更多详细信息:
http://nshipster.com/uuid-udid-unique-identifier/>
Swift 4.1 的更新,您需要使用:
UIDevice.current.identifierForVendor?.uuidString
How can I get a device's unique ID in Swift?
I need an ID to use in the database and as the API-key for my web service in my social app. Something to keep track of this devices daily use and limit its queries to the database.
You can use this (Swift 3):
UIDevice.current.identifierForVendor!.uuidString
For older versions:
UIDevice.currentDevice().identifierForVendor
or if you want a string:
UIDevice.currentDevice().identifierForVendor!.UUIDString
There is no longer a way to uniquely identify a device after the user uninstalled the app(s). The documentation says:
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.
You may also want to read this article by Mattt Thompson for more details:
http://nshipster.com/uuid-udid-unique-identifier/
Update for Swift 4.1, you will need to use:
UIDevice.current.identifierForVendor?.uuidString
这篇关于如何在 Swift 中获取唯一的设备 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!