ios 如何在 Swift 中获取唯一的设备 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25925481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a unique device ID in Swift?
提问by User
How can I get a devices unique ID in Swift?
如何在 Swift 中获取设备唯一 ID?
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.
我需要一个 ID 以在数据库中使用,并作为我社交应用程序中 Web 服务的 API 密钥。用于跟踪此设备的日常使用情况并将其查询限制在数据库中。
Thanks!
谢谢!
回答by Atomix
You can use this (Swift 3):
你可以使用这个(Swift 3):
UIDevice.current.identifierForVendor!.uuidString
For older versions:
对于旧版本:
UIDevice.currentDevice().identifierForVendor
or if you want a string:
或者如果你想要一个字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString
There 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.
当应用程序(或来自同一供应商的另一个应用程序)安装在 iOS 设备上时,此属性中的值保持不变。当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值会发生变化。
You may also want to read this article by Mattt Thompson for more details:
http://nshipster.com/uuid-udid-unique-identifier/
您可能还想阅读 Mattt Thompson 的这篇文章以了解更多详细信息:http://nshipster.com/uuid-udid-unique-identifier/
Update for Swift 4.1, you will need to use:
更新 Swift 4.1,您将需要使用:
UIDevice.current.identifierForVendor?.uuidString
回答by Jayprakash Dubey
You can use identifierForVendor public property present in UIDevice class
您可以使用 UIDevice 类中存在的 identifierForVendor 公共属性
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
EDITSwift 3:
编辑斯威夫特 3:
UIDevice.current.identifierForVendor!.uuidString
END EDIT
结束编辑
回答by SwiftDeveloper
For Swift 3.XLatest Working Code, Easily usage;
适用于Swift 3.X最新工作代码,易于使用;
let deviceID = UIDevice.current.identifierForVendor!.uuidString
print(deviceID)
回答by iluvatar_GR
You can use devicecheck(in Swift 4) Apple documentation
您可以使用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" ...
}
}
Typical usage:
典型用法:
Typically, you use the DeviceCheck APIs to ensure that a new user has not already redeemed an offer under a different user name on the same device.
通常,您使用 DeviceCheck API 来确保新用户尚未在同一设备上以不同用户名兑换优惠。
Server action needs:
服务器操作需要:
more from Santosh Botre article - Unique Identifier for the iOS Devices
更多来自 Santosh Botre 文章 - iOS 设备的唯一标识符
Your associated server combines this token with an authentication key that you receive from Apple and uses the result to request access to the per-device bits.
您的关联服务器将此令牌与您从 Apple 收到的身份验证密钥组合在一起,并使用结果请求访问每个设备的位。
回答by n.by.n
Swift 2.2
斯威夫特 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")!)
回答by Davender Verma
if (UIDevice.current.identifierForVendor?.uuidString) != nil
{
self.lblDeviceIdValue.text = UIDevice.current.identifierForVendor?.uuidString
}
回答by Joey
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)
}
}
}
回答by Shihab
The value of identifierForVendorchanges when the user deletes all of that vendor's apps from the device, if you want to keep the unique ID even for the subsequent fresh installations, you can try to use the following function
当用户从设备中删除所有该供应商的应用程序时,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
}
}
回答by Matt
I've tried with
我试过
let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString
instead
反而
let UUID = NSUUID().UUIDString
and it works.
它有效。