【问题标题】:How to get unique id in iOS device?如何在 iOS 设备中获取唯一 ID?
【发布时间】:2013-10-16 11:34:49
【问题描述】:

我已经使用 mac 地址来识别服务器端的 iOS 设备。当使用 iOS 7 运行我的应用程序时,无法检索正确的 mac 地址。或者我使用了

NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
[strApplicationUUID setString:[oNSUUID UUIDString]];

属性。但是在每次新安装应用程序后,这个值也会发生变化。现在我想在服务器端检测特定设备?如何在 iOS 中获取每个设备的唯一 ID?

【问题讨论】:

    标签: ios


    【解决方案1】:

    您无法再为每台设备获取唯一 ID。 identifierForVendor 是您将得到的最好的。 Apple 系统性地禁止识别特定设备,以便营销人员无法传递用户 ID。

    要将标识符ID作为字符串获取,可以使用

    let deviceId = UIDevice.current.identifierForVendor?.uuidString
    

    更新

    如果您想要一个通用 ID,那么您可以选择使用 advertisingIdentifier。但是,如果用户在其设备设置中限制了广告跟踪,则该值将简单地返回全零。

    import AdSupport
    
    let identifierManager = ASIdentifierManager.shared()
    if identifierManager.isAdvertisingTrackingEnabled {
        let deviceId = identifierManager.advertisingIdentifier.uuidString
    }
    

    注意 Apple 建议此代码仅供构建广告框架的公司使用,而不是应用程序开发人员自己使用。如果您在代码中将其用于与广告无关的目的,请准备好拒绝您的应用。

    【讨论】:

    • 通过stackoverflow.com/questions/19402327/…快速获取设备ID
    • 如果有其他人像我一样从 libgdx/robovm 土地来寻找用 Java 执行此操作的方法:UIDevice device = UIDevice.getCurrentDevice(); String id = device.getIdentifierForVendor().asString();
    【解决方案2】:

    获取设备ID的最佳方式是identifierForVendor

    UIDevice *device = [UIDevice currentDevice];
    
    NSString  *currentDeviceId = [[device identifierForVendor]UUIDString];
    

    更新

    对于 Swift 4.1 使用

    UIDevice.current.identifierForVendor?.uuidString
    

    【讨论】:

      【解决方案3】:

      获取设备ID

      斯威夫特 3

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

      斯威夫特 4:

      let device_id = UIDevice.current.identifierForVendor!.uuidString
      

      【讨论】:

      • 嗨..在我的应用程序中,我需要获取设备 ID。苹果支持获取设备ID吗?我们可以将应用提交到应用商店
      • 嗨@hardik 感谢您的回复。在设备上进行测试时,我开始知道如果我每次都卸载并重新安装应用程序,我会得到不同的 UUID。一旦用户删除我们的应用程序,我们就无法获得相同的 ID。
      • 嗨@hardik 有没有办法获得相同的UDID?
      • @Jayesh 使用哪种语言 swift 或 Obj- c 如果 swift 比这种方法更直接,否则你可以获得 1) let UUID = NSUUID().UUIDString
      • swift 3.0 更新:let deviceID = UIDevice.current.identifierForVendor?.uuidString
      【解决方案4】:

      在 Swift 3.x 中获取设备 ID

      let deviceId = UIDevice.current.identifierForVendor?.uuidString
      

      【讨论】:

        【解决方案5】:

        你可以使用这个库:https://github.com/fabiocaccamo/FCUUID

        请查看持久性部分的表格。 uuidForDevice 将涵盖大部分需要的案例,它是旧版本 iOS udid 的最佳替代品。

        Readme 中的示例在 Objective-C 中,但它也适用于 Swift。从 Swift 第一次发布到现在(Swift 4.1),我一直在使用它。

        【讨论】:

          【解决方案6】:

          对于 Swift 5

          let deviceId = UIDevice.current.identifierForVendor?.uuidString
          

          【讨论】:

            猜你喜欢
            • 2017-01-08
            • 2017-09-07
            • 2014-11-13
            • 1970-01-01
            • 2020-06-15
            • 2020-12-29
            • 2017-01-06
            • 1970-01-01
            • 2019-08-28
            相关资源
            最近更新 更多