如何在Swift中获取唯一的设备ID?

问题描述 投票:144回答:7

如何在Swift中获取设备唯一ID?

我需要在数据库中使用ID,并在社交应用程序中作为我的Web服务的API密钥。跟踪日常使用的设备并将其查询限制在数据库中的东西。

谢谢!

swift ios8 uuid uniqueidentifier
7个回答
343
投票

你可以使用这个(Swift 3):

UIDevice.current.identifierForVendor!.uuidString

对于旧版本:

UIDevice.currentDevice().identifierForVendor

或者如果你想要一个字符串:

UIDevice.currentDevice().identifierForVendor!.UUIDString

在用户卸载应用程序后,不再能够唯一地识别设备。文件说:

在iOS设备上安装app(或来自同一供应商的其他应用程序)时,此属性中的值保持不变。当用户从设备中删除所有该供应商的应用程序并随后重新安装其中一个或多个应用程序时,该值会更改。

您可能还想阅读Mattt Thompson的这篇文章了解更多详情: http://nshipster.com/uuid-udid-unique-identifier/

更新Swift 4.1,您需要使用:

UIDevice.current.identifierForVendor?.uuidString

9
投票

您可以使用UIDevice类中存在的identifierForVendor公共属性

let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
        print("UUID: \(UUIDValue)")

编辑Swift 3:

UIDevice.current.identifierForVendor!.uuidString

结束编辑

Screenshot for property hierarchy


9
投票

适用于Swift 3.X最新工作代码,易于使用;

   let deviceID = UIDevice.current.identifierForVendor!.uuidString
   print(deviceID)

8
投票

您可以使用devicecheck(在Swift 4中)Apple documentation

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确保新用户尚未在同一设备上以其他用户名兑换商品。

服务器动作需要:

See WWDC 2017 — Session 702

more from Santosh Botre article - Unique Identifier for the iOS Devices

您的关联服务器将此令牌与您从Apple收到的身份验证密钥组合在一起,并使用该结果请求访问每个设备位。


2
投票

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")!)

0
投票
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)
        }
    }
}

-15
投票

我试过了

let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString

代替

let UUID = NSUUID().UUIDString

它的工作原理。

© www.soinside.com 2019 - 2024. All rights reserved.