【问题标题】:Retrieve iOS device identifier检索 iOS 设备标识符
【发布时间】:2012-12-20 12:40:28
【问题描述】:

有没有办法在 iOS SDK 中获取 iOS 设备标识符? 我想访问 Xcode 在 Organizer - Devices 部分中提供的标识符,例如:21xb1fxef5x2052xec31x3xd3x48ex5e437xe593

【问题讨论】:

  • 为什么要获取那个标识符?
  • 我需要唯一标识设备

标签: iphone ios uniqueidentifier


【解决方案1】:

看起来你仍然可以在 iOS 6 下访问 UDID,但 它已被弃用,因为 iOS 5.0 并且你不应该使用它(无论如何你会收到警告)

[UIDevice currentDevice].uniqueIdentifier

如果您需要唯一标识符,您应该使用:

[UIDevice currentDevice].identifierForVendor

或者如果它与某种广告有关,那么:

// from  AdSupport.framework
[ASIdentifierManager sharedManager].advertisingIdentifier

但是,这两个新属性仅在 iOS >= 6.0 下可用,而且 adsIdentifier 并不是真正唯一的(我从中得到了很多重复项)。

如果你不支持 iOS

UIDevice *device = [UIDevice currentDevice];
NSString *ident = nil;
if ([device respondsToSelector:SEL(identifierForVendor)]) {
    ident = [device.identifierForVendor UUIDString];
} else {
    ident = device.uniqueIdentifier;
}

但我不确定苹果在审核期间会如何回应。

您还可以使用一些 3rd 方解决方案,例如 openUDIDsecureUDID。不推荐使用开放且安全的 UDID - 使用供应商/广告标识符。


更新

另一种可能性是使用 MAC 地址作为唯一哈希的基础,例如您可以使用来自 ODIN1 - source is here 的代码

从 iOS7 开始,MAC 地址不再可用。 (人们可以阅读它,但它总是相同的虚拟地址 02:00:00:00:00:00)。

【讨论】:

  • 该属性在 iOS 5.0 中已弃用
  • 是的,抱歉,起初我写的是 uniqueIdentifier 而不是 identifierForVendor,但无论如何我认为当我写你不应该使用 uniqueIdentifier 而在第二行你应该使用它;)
  • Crashlytics 不再支持 SecureUDID;他们写道:“我们建议改用 Apple 的 iOS 6 供应商和广告标识符。”。
  • 谢谢,我也更新了我的答案,MAC 地址自 iOS7 以来不可用
【解决方案2】:

来自Apple Documentation

基于各种硬件的每个设备唯一的字母数字字符串 细节。 (只读)(在 iOS 5.0 中已弃用。使用 此类的 identifierForVendor 属性或 ASIdentifierManager 类的广告标识符属性 而是酌情使用 NSUUID 类的 UUID 方法 创建一个 UUID 并将其写入用户默认数据库。)

【讨论】:

    【解决方案3】:
    NSString* identifier = nil;
    if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) {
      // iOS 6+
      identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    } else {
      // before iOS 6, so just generate an identifier and store it
      identifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"identiferForVendor"];
      if( !identifier ) {
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        identifier = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
        [[NSUserDefaults standardUserDefaults] setObject:identifier forKey:@"identifierForVendor"];
      }
    }
    

    【讨论】:

      【解决方案4】:

      您可以找到唯一的设备标识符

      [[UIDevice currentDevice] uniqueIdentifier]
      

      【讨论】:

        【解决方案5】:
        + (NSString *)uuid
        {
            NSString *uuidString = nil;
            CFUUIDRef uuid = CFUUIDCreate(NULL);
            if (uuid) {
                uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
                CFRelease(uuid);
            }
            return [uuidString autorelease];
        }
        

        它可以 100% 工作,即使在模拟器上也是如此......

        【讨论】:

          【解决方案6】:

          是的。

          [[UIDevice currentDevice] uniqueIdentifier]
          

          编辑: 但是,这在 iOS 5 中已被弃用。此标识符不应再在 iOS 5 中使用。阅读this SO 帖子了解更多详细信息。

          【讨论】:

          • 这在 iOS 5 中已被弃用,使用它的应用程序将不允许进入 App Store。
          猜你喜欢
          • 2013-10-07
          • 2019-04-25
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          • 2011-11-13
          相关资源
          最近更新 更多