Ios 如何在Swift中获取唯一的设备ID?
如何在Swift中获取设备的唯一ID 我需要一个ID在数据库中使用,并作为社交应用程序中web服务的API密钥。用于跟踪此设备日常使用情况并将其查询限制在数据库中的内容。您可以使用此功能(Swift 3): 对于旧版本:Ios 如何在Swift中获取唯一的设备ID?,ios,swift,uuid,uniqueidentifier,Ios,Swift,Uuid,Uniqueidentifier,如何在Swift中获取设备的唯一ID 我需要一个ID在数据库中使用,并作为社交应用程序中web服务的API密钥。用于跟踪此设备日常使用情况并将其查询限制在数据库中的内容。您可以使用此功能(Swift 3): 对于旧版本: UIDevice.currentDevice().identifierForVendor 或者,如果需要字符串: UIDevice.currentDevice().identifierForVendor!.UUIDString 在用户卸载应用程序后,不再存在唯一标识设备的
UIDevice.currentDevice().identifierForVendor
或者,如果需要字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString
在用户卸载应用程序后,不再存在唯一标识设备的方法。文件说: 在iOS设备上安装应用程序(或来自同一供应商的其他应用程序)时,此属性中的值保持不变。当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值将发生变化
您可能还想阅读马特·汤普森的这篇文章以了解更多细节:
对于Swift 4.1的更新,您需要使用:
UIDevice.current.identifierForVendor?.uuidString
您可以使用UIDevice类中存在的identifierForVendor公共属性
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
编辑
Swift 3:
结束编辑
Swift 2.2
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let userDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.objectForKey("ApplicationIdentifier") == nil {
let UUID = NSUUID().UUIDString
userDefaults.setObject(UUID, forKey: "ApplicationIdentifier")
userDefaults.synchronize()
}
return true
}
//Retrieve
print(NSUserDefaults.standardUserDefaults().valueForKey("ApplicationIdentifier")!)
我试过了
let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString
反而
let UUID = NSUUID().UUIDString
它可以工作。适用于Swift 3.X最新的工作代码,使用方便
let deviceID = UIDevice.current.identifierForVendor!.uuidString
print(deviceID)
您可以使用设备检查(在Swift 4中) 典型用法: 通常,您使用DeviceCheck API来确保新用户尚未在同一设备上以不同用户名兑换优惠 服务器操作需要: (24:06) 您的关联服务器将此令牌与您从Apple收到的身份验证密钥相结合,并使用结果请求访问每设备位
也许可以添加.uuistring,这样它就可以被发送到服务器了?是的。我想这是显而易见的事情,但我在答案中包含了它。卸载应用程序后,这个数字不再相同。有没有一种方法可以在应用程序卸载后跟踪设备?更好的问题是,如果卸载后设备被删除,当ID不再使用时,你如何删除他们在服务器上的帐户?@UmairAfzal很多东西在模拟器中不起作用。您在真实设备上尝试过吗?nsuid().UUIDString每次都会给您一个新字符串如果我理解正确,令牌生成是使用DCDevice.generateToken()执行的,每次调用都会生成一个唯一的随机设备令牌。因此,设备令牌不是持久的,而是短暂的。我不明白的是服务器如何将临时令牌与设备关联。@user1118764“设置两位 — Application server将负责共享此令牌并设置位值。“请在此处和此处阅读官方文档和702会议的更多信息,FYI 24:06是WWDC 2017会议702^中讨论设备上的幻灯片检查的地方。有没有一种方法可以在没有服务器向苹果发出额外请求的情况下执行此操作?
generateToken
中的数据令牌是否不足?每当再次使用卸载和安装应用程序时,这会发生变化。这只是@Atomix&JayprakasDubey的答案的副本。您可以使用FCUUID pod()这允许您获得一个uuid,即使在删除应用程序后也不会更改。这只是@Atomix Answer的副本。您可以使用FCUUID pod()这允许您获取uuid,该uuid即使在删除应用程序后也不会更改。如果用户删除应用程序,则该uuid无效,因为在卸载应用程序时,UserDefault也会被删除。请评论您的代码和/或提供详细信息,并解释如何解决发布的问题。
let UUID = NSUUID().UUIDString
let deviceID = UIDevice.current.identifierForVendor!.uuidString
print(deviceID)
func sendEphemeralToken() {
//check if DCDevice is available (iOS 11)
//get the **ephemeral** token
DCDevice.current.generateToken {
(data, error) in
guard let data = data else {
return
}
//send **ephemeral** token to server to
let token = data.base64EncodedString()
//Alamofire.request("https://myServer/deviceToken" ...
}
}
class func uuid(completionHandler: @escaping (String) -> ()) {
if let uuid = UIDevice.current.identifierForVendor?.uuidString {
completionHandler(uuid)
}
else {
// 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.
// https://developer.apple.com/documentation/uikit/uidevice/1620059-identifierforvendor?language=objc
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
uuid(completionHandler: completionHandler)
}
}
}
if (UIDevice.current.identifierForVendor?.uuidString) != nil
{
self.lblDeviceIdValue.text = UIDevice.current.identifierForVendor?.uuidString
}