当我得到from itunes并以编程方式这样时,我为我的iphone获得不同的UDID
UDID:String = UIDevice.current.identifierForVendor!.uuidString
基本上我试图获取我的iPhone的唯一标识符,就像我们有Android手机的mac地址。
最简单的方法是通过在keychain中存储identifierForVendor来解决这个问题。即使您卸载应用程序,密钥的值保持不变并且保持不变。许多第三方库可以执行此操作。其中一个https://github.com/jrendel/SwiftKeychainWrapper。
func getGlobalUniqueIdentifierFromKeyChain()->String{
let retrievedString: String? = KeychainWrapper.standard.string(forKey: "DeviceId")
if retrievedString == nil{
if let deviceKey = UIDevice.current.identifierForVendor?.uuidString{
let _ = KeychainWrapper.standard.set(deviceKey, forKey: "DeviceId")
}
}
if let globalID = KeychainWrapper.standard.string(forKey: "DeviceId"){
return globalID
}else{
return UIDevice.current.identifierForVendor?.uuidString ?? ""
}
}