Answers:
您可以使用此(快速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)")
编辑 Swift 3:
UIDevice.current.identifierForVendor!.uuidString
结束编辑
对于Swift 3.X最新工作代码,易于使用;
let deviceID = UIDevice.current.identifierForVendor!.uuidString
print(deviceID)
您可以使用devicecheck(在Swift 4中) Apple文档
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来确保新用户尚未在同一设备上以其他用户名兑换优惠。
服务器操作需要:
Santosh Botre文章中的更多内容-iOS设备的唯一标识符
您关联的服务器将此令牌与您从Apple收到的身份验证密钥结合在一起,并使用结果请求访问每设备位。
斯威夫特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")!)
if (UIDevice.current.identifierForVendor?.uuidString) != nil
{
self.lblDeviceIdValue.text = UIDevice.current.identifierForVendor?.uuidString
}
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)
}
}
}
当用户从设备上删除该供应商的所有应用程序时,identifierForVendor的值将更改,如果您甚至在以后的全新安装中也要保留唯一的ID,则可以尝试使用以下功能
func vendorIdentifierForDevice()->String {
//Get common part of Applicatoin Bundle ID, Note : getCommonPartOfApplicationBundleID is to be defined.
let commonAppBundleID = getCommonPartOfApplicationBundleID()
//Read from KeyChain using bunndle ID, Note : readFromKeyChain is to be defined.
if let vendorID = readFromKeyChain(commonAppBundleID) {
return vendorID
} else {
var vendorID = NSUUID().uuidString
//Save to KeyChain using bunndle ID, Note : saveToKeyChain is to be defined.
saveToKeyChain(commonAppBundleID, vendorID)
return vendorID
}
}