【问题标题】:How to get Device UDID in programmatically in iOS7?如何在 iOS7 中以编程方式获取设备 UDID?
【发布时间】:2014-01-23 13:26:01
【问题描述】:

如何在 iOS7 中以编程方式获取设备 UDID。[[UIDevice currentDevice] uniqueIdentifier] 我曾使用此代码 这是已弃用的 iOS7。如何获取设备 UDID。当我删除应用程序并重新安装时,UDID 字符串发生了变化,这意味着 UDID 变得不同了。

【问题讨论】:

标签: iphone udid


【解决方案1】:

在 iOS 7 中,Apple 现在在查询 MAC 时始终返回一个固定值,以专门阻止将 MAC 作为 ID 方案的基础。所以你现在真的应该使用-[UIDevice identifierForVendor] 或创建一个每次安装的 UUID。

检查this SO 问题。

【讨论】:

  • 我使用了这个代码 [UIDvice identifierForVendor] 但是当我运行我的应用程序时它返回 ID,然后删除并重新运行我的应用程序它将返回相应的 id。它会生成随机id。
【解决方案2】:

由于安全/隐私原因,UDID 在 iOS 6+ 中不再可用。相反,请使用 identifierForVendor 或 adsIdentifier。

请通过this链接。

   NSString* uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
   NSLog(@"UDID:: %@", uniqueIdentifier);

iOS 8+ 更新

+ (NSString *)deviceUUID
{
    if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
        return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];

    @autoreleasepool {

        CFUUIDRef uuidReference = CFUUIDCreate(nil);
        CFStringRef stringReference = CFUUIDCreateString(nil, uuidReference);
        NSString *uuidString = (__bridge NSString *)(stringReference);
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
        [[NSUserDefaults standardUserDefaults] synchronize];
        CFRelease(uuidReference);
        CFRelease(stringReference);
        return uuidString;
    }
}

【讨论】:

  • 是的,我在上面的代码中使用过。但是当我删除然后重新安装我的应用程序时,会生成不同的 UDID。它会动态变化。
  • 上面的例子没有给出正确的值作为设备 UDID,而且值正在逐个系统地改变
  • 我相信这是苹果的意图。如果用户有理由删除应用程序,他们应该不再是可追踪的。
  • 别担心,我也有解决方案。使用钥匙串... :)
【解决方案3】:

所有版本都 100% 有效

苹果推荐的其他最佳选择使用ASIdentifierManager Class Reference

ios7-app-backward-compatible-with-ios5-regarding-unique-identifier

这个链接告诉你如何处理和使用自定义框架

uidevice-uniqueidentifier-property-is-deprecated-what-now

iOS 9

NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"20B0DDE7-6087-4607-842A-E97C72E4D522"];
NSLog(@"%@",uuid);
NSLog(@"%@",[uuid UUIDString]);

或

只支持ios 6.0及以上版本

使用代码[[[UIDevice currentDevice] identifierForVendor] UUIDString];

NSUUID *deviceId;
#if TARGET_IPHONE_SIMULATOR
deviceId = [NSUUID initWithUUIDString:@"UUID-STRING-VALUE"];
#else
deviceId = [UIDevice currentDevice].identifierForVendor;
#endif

ios 5 使用喜欢

 if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
    // This is will run if it is iOS6
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
   // This is will run before iOS6 and you can use openUDID or other 
   // method to generate an identifier
}

【讨论】:

  • [NSUUID initWithUUIDString:@"UUID-STRING-VALUE"];缺少分配。
  • 我正在 iOS 9 上进行测试并得到“选择器 'initWithUUIDString' 的未知类方法”。为什么会这样?
  • 从诸如“E621E1F8-C36C-495A-93FC-0C247A3E6E5F”之类的字符串创建一个 NSUUID。对无效字符串返回 nil。 @JayprakashDubey
  • 你可以使用 alloc NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"20B0DDE7-6087-4607-842A-E97C72E4D522"]; @JayprakashDubey
  • 对于ASIdentifierManager,如果你不提供广告,你会被AppStore拒绝吗?
【解决方案4】:

使用 identifierForVendor 或 adsIdentifier。

identifierForVendor:

An alphanumeric string that uniquely identifies a device to the app’s vendor. (read-only)

The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.

广告标识符:

An alphanumeric string unique to each device, used only for serving advertisements. (read-only)

Unlike the identifierForVendor property of UIDevice, the same value is returned to all vendors. This identifier may change—for example, if the user erases the device—so you should not cache it.

另外,有关 identifierForVendor 和 adsIdentifier 的信息,请参阅 Apple 文档。

【讨论】:

    【解决方案5】:

    有两种解决方案:

    • 您可以使用 identifierForVendor 之后将它们存储到钥匙串并稍后使用。因为重新安装应用时钥匙串的值不会改变。
    • 你可以试试OpenUDID

    【讨论】:

      【解决方案6】:

      在 Swift 中,您可以像这样获取设备 UUID

      let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
      println(uuid)
      

      【讨论】:

      • 他询问的是 UDID,而不是 UUID。
      【解决方案7】:

      获取 UDID:(如果你使用这个可能是 App Store 的人不会允许的 --> 根据我的担心)

      - (NSString *)udid
      {
      void *gestalt = dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY);
      CFStringRef (*MGCopyAnswer)(CFStringRef) = (CFStringRef (*)(CFStringRef))(dlsym(gestalt, "MGCopyAnswer"));
      return CFBridgingRelease(MGCopyAnswer(CFSTR("UniqueDeviceID")));
      }
      
       **Entitlements:**
       <key>com.apple.private.MobileGestalt.AllowedProtectedKeys</key>
         <array>
      <string>UniqueDeviceID</string>
      </array>
      

      获取 UUID:

          self.uuidTxtFldRef.text = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
      

      【讨论】:

      • 似乎 UDID 代码现在不起作用。你能确认一下吗?我在 iOS 10.2 设备上尝试了 Xcode 8.2.1。
      【解决方案8】:

      Swift 2.2+ 获取 UUID 的正确方法:

      if let UUID = UIDevice.currentDevice().identifierForVendor {
        print("UUID: \(UUID.UUIDString)")
      }
      

      【讨论】:

        【解决方案9】:

        在 Swift 3.0 中获取 UUID:

        let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
        

        【讨论】:

          【解决方案10】:

          在 Swift 3.0 中:

          UIDevice.current.identifierForVendor!.uuidString
          

          旧版本

          UIDevice.currentDevice().identifierForVendor
          

          你想要一个字符串:

          UIDevice.currentDevice().identifierForVendor!.UUIDString
          

          【讨论】:

            猜你喜欢
            • 2015-10-17
            • 2014-03-14
            • 1970-01-01
            • 2013-09-26
            • 1970-01-01
            • 2020-04-05
            • 1970-01-01
            • 2010-09-11
            • 2016-10-19
            相关资源
            最近更新 更多