如何在Swift中获取设备唯一的ID?
我需要一个ID在数据库中使用,并作为我的社交应用程序中的Web服务的API密钥。 某些东西要跟踪这个设备的日常使用,并将其查询限制到数据库。
谢谢!
How can I get a devices 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.
Thanks!
最满意答案
你可以使用(Swift 3):
UIDevice.current.identifierForVendor!.uuidString对于旧版本:
UIDevice.currentDevice().identifierForVendor或者如果你想要一个字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString在用户卸载该应用程序之后,不再有唯一标识设备的方法。 文件说:
在iOS设备上安装应用程序(或同一供应商的其他应用程序)时,该属性的值保持不变。 当用户从设备中删除所有供应商的应用程序并随后重新安装一个或多个应用程序时,该值会发生变化。
您可能还想阅读Mattt Thompson的这篇文章了解更多详情: http://nshipster.com/uuid-udid-unique-identifier/
You can use this (Swift 3):
UIDevice.current.identifierForVendor!.uuidStringFor older versions:
UIDevice.currentDevice().identifierForVendoror if you want a string:
UIDevice.currentDevice().identifierForVendor!.UUIDStringThere 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更多推荐
发布评论