【问题标题】:How to get a unique device ID in Swift?如何在 Swift 中获取唯一的设备 ID?
【发布时间】:2014-11-13 13:12:41
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift uuid uniqueidentifier


    【解决方案1】:

    你可以使用这个(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
    

    【讨论】:

    • 也许添加 .UUIDString 以便它可以实际发送到服务器?
    • 是的。我认为这是显而易见的事情,但我将其包含在答案中。
    • 应用卸载后这个数字不再一样了。有没有办法在应用卸载后跟踪设备?
    • 更好的问题是,如果卸载后将其删除,当 ID 不再使用时,如何在服务器上删除他们的帐户?
    • @UmairAfzal 很多东西在模拟器中不起作用。你在真机上试过了吗?
    【解决方案2】:

    您可以使用 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 (24:06)

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

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

    【讨论】:

    • 如果我理解正确的话,令牌生成是使用 DCDevice.generateToken() 执行的,并且每次调用都会生成一个唯一的、随机的 device_token。因此, device_token 不是持久的,而是短暂的。我不明白的是服务器如何能够将临时令牌与设备相关联。
    • @user1118764 "设置两位 — 应用服务器将负责共享此令牌并设置位值。"在此处阅读更多内容medium.com/@santoshbotre01/… 以及官方文档developer.apple.com/documentation/devicecheck/… 和702 会话developer.apple.com/videos/play/wwdc2017/702
    • 仅供参考 24:06 是 WWDC 2017 会议 702 中讨论 DeviceCheck 幻灯片的地方^
    • 有没有办法在没有服务器向苹果发出额外请求的情况下做到这一点?来自generateToken 的数据令牌还不够吗?
    【解决方案3】:

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

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

    【讨论】:

    • 每当使用卸载并再次安装应用程序时,此情况都会发生变化。
    • 这只是@Atomix & JayprakasDubey 答案的副本
    • 您可以使用 FCUUID pod (github.com/fabiocaccamo/FCUUID) 获取即使在删除应用后也不会更改的 uuid。
    【解决方案4】:

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

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

    编辑 斯威夫特 3:

    UIDevice.current.identifierForVendor!.uuidString
    

    结束编辑

    【讨论】:

    • 这只是@Atomix 答案的副本
    • 您可以使用 FCUUID pod (github.com/fabiocaccamo/FCUUID) 获取即使在删除应用后也不会更改的 uuid。
    【解决方案5】:

    斯威夫特 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")!)
    

    【讨论】:

    • 如果用户删除应用程序是没有用的,因为用户默认值也会随着应用程序卸载而被删除。
    【解决方案6】:
    if (UIDevice.current.identifierForVendor?.uuidString) != nil
            {
                self.lblDeviceIdValue.text = UIDevice.current.identifierForVendor?.uuidString
            }
    

    【讨论】:

    • 请评论您的代码和/或提供详细信息并解释如何解决发布的问题。
    【解决方案7】:
    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)
            }
        }
    }
    

    【讨论】:

      【解决方案8】:

      我试过了

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

      改为

      let UUID = NSUUID().UUIDString
      

      它有效。

      【讨论】:

      • NSUUID().UUIDString 每次都会给你一个新字符串
      猜你喜欢
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 2020-06-15
      • 2017-01-06
      • 2019-08-28
      • 1970-01-01
      • 2017-09-07
      • 2015-11-15
      相关资源
      最近更新 更多