如何在Swift中获得设备的唯一ID ?
我需要一个ID在数据库中使用,并在我的社交应用程序中作为我的web服务的API-key。一些东西来跟踪这个设备的日常使用情况,并限制其对数据库的查询。
如何在Swift中获得设备的唯一ID ?
我需要一个ID在数据库中使用,并在我的社交应用程序中作为我的web服务的API-key。一些东西来跟踪这个设备的日常使用情况,并限制其对数据库的查询。
你可以使用这个(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
你可以在UIDevice类中使用identifierForVendor公共属性
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
编辑 斯威夫特3:
UIDevice.current.identifierForVendor!.uuidString
最后编辑
斯威夫特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)
你可以使用devicecheck(在Swift 4中) 苹果公司的文档
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" ...
}
}
典型的用法:
通常,您使用DeviceCheck api来确保新用户没有在同一设备上以不同的用户名赎回优惠。
服务器动作需求:
参见WWDC 2017 -第702场(24:06)
更多来自Santosh Botre文章- iOS设备的唯一标识符
您的相关服务器将此令牌与您从Apple收到的验证密钥结合起来,并使用结果请求访问每个设备位。
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
}
在swift 4,5 您可以使用下面的代码获取UUID。
print(UIDevice.current.identifierForVendor!.uuidString)
输出
除此之外,您可以从连接的设备获得多个属性。
UIDevice.current.name // e.g. "My iPhone"
UIDevice.current.model // e.g. @"iPhone", @"iPod touch"
UIDevice.current.localizedModel // localized version of model
UIDevice.current.systemName // e.g. @"iOS"
UIDevice.current.systemVersion // e.g. @"15.5"