【问题标题】:How to get the Device ID from an iOS app?如何从 iOS 应用程序获取设备 ID?
【发布时间】:2011-03-29 06:17:12
【问题描述】:

如何在 iOS 应用中获取用户设备的唯一 ID?

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    使用这个

        UIDevice *device = [UIDevice currentDevice];
        NSString *uniqueIdentifier = [device uniqueIdentifier];
    

    更新

    Apple 具有已弃用的唯一标识符,因此现在以下代码(来自 Melvin Sovereign 的评论)是合适的:

    NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    

    【讨论】:

    • 那么你现在应该使用什么?还是根本无法识别唯一的设备?
    • 新的ios5+方式:[[[UIDevice currentDevice] identifierForVendor] UUIDString]
    • 我认为每次获得 identifierForVendor 时都会得到不同的值
    • 是的,每次重新安装应用程序都会获得新的 UUID
    • 应该缓存结果 UUIDString 还是获取成本低廉?
    【解决方案2】:

    有趣的是,Apple 已经在 iOS 5 中弃用了 uniqueIdentifier。下面是相关的 TechCrunch 文章: http://techcrunch.com/2011/08/19/apple-ios-5-phasing-out-udid/

    Apple 建议您不再唯一标识设备,而是标识用户。在大多数情况下,这是一个很好的建议,尽管在某些情况下仍需要全局唯一的设备 ID。这些场景在广告中很常见。因此,我编写了一个非常简单的插件库,它准确地复制了现有的行为。

    在一个无耻的自我推销中,我将它链接在这里,希望有人觉得它有用。此外,我欢迎所有和任何反馈/批评: http://www.binpress.com/app/myid/591

    【讨论】:

      【解决方案3】:

      我认为这段代码可能会对你有所帮助;)

      NSString * id = [UIDevice currentDevice].uniqueIdentifier;

      你也可以去http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html看看

      【讨论】:

      • 有趣的是,Apple 已经在 iOS 5 中弃用了 uniqueIdentifier。这是相关的 TechCrunch 文章:techcrunch.com/2011/08/19/apple-ios-5-phasing-out-udidApple 建议您不再唯一标识设备,而是标识 用户。在大多数情况下,这是一个很好的建议,尽管有些情况需要全局唯一的设备 ID。这些场景是很常见的广告。
      • 在 iOS 7+ 中使用: NSString *identifer = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
      【解决方案4】:

      您可以使用NSUUID *identifierForVendor

      [[UIDevice currentDevice] identifierForVendor]
      

      【讨论】:

        【解决方案5】:

        在斯威夫特中

        var uniqueId=UIDevice.currentDevice().identifierForVendor.UUIDString as String
        println("Your device identifires =>\(uniqueId)")
        

        【讨论】:

          【解决方案6】:

          我知道,这是一个很老的问题,但这就是我解决问题的方法。如果创建“唯一设备令牌”的线程永远不会停止,它可能会失败,但它对我有用。

          -(NSString*)getUniqueDeviceToken
          {
              __block NSString* UDTToReturn = @"UDTCouldNotBeCreatedSuccessfully,PlatformNotSupported,Simulator,iOSVer<11";
              dispatch_semaphore_t semaphoretowaitforudtcreation = dispatch_semaphore_create(0);
              if ([DCDevice.currentDevice isSupported])
              {
                  [DCDevice.currentDevice generateTokenWithCompletionHandler:^(NSData * _Nullable token, NSError * _Nullable error)
                  {
                      if (error)
                      {
                          UDTToReturn = error.description;
                          dispatch_semaphore_signal(semaphoretowaitforudtcreation);
                      }
                  
                      else
                      {
                          UDTToReturn = [token base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
                          dispatch_semaphore_signal(semaphoretowaitforudtcreation);
                      }
                  }];
              }
          dispatch_semaphore_wait(semaphoretowaitforudtcreation, DISPATCH_TIME_FOREVER);
          return UDTToReturn;
          }
          

          【讨论】:

          • 这段代码在做什么?它是获取设备的唯一 ID,还是只生成一个 uuid?另外:有 Swift 版本吗?
          • 我创建/使用此代码来创建“唯一设备令牌”并将其保存到我的 iOS 设备(iPhone、iPad)上的本地文件中。苹果从 201 年起就不允许开发者使用“uuid”了? .以某种方式,您可以说它创建了一个特定于设备的编号,但当您在设备上重新安装应用程序时,它可能会发生变化。该代码仅存在于 obj-c-code 中,但也可以/易于在 swift 中使用。整个“识别设备”主题并不容易理解。如果你想要你的应用程序的某种“使用信息”,你必须注册到“APNs”。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-25
          相关资源
          最近更新 更多