如何在iOS7中以编程方式获取Device UDID?

时间:2014-01-06 07:18:12

标签: iphone udid

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

10 个答案:

答案 0 :(得分:34)

对所有版本

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
}

答案 1 :(得分:19)

出于安全/隐私原因,iOS 6+中不再提供UDID。相反,请使用identifierForVendor或advertisingIdentifier。

请浏览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;
    }
}

答案 2 :(得分:6)

在Swift中你可以得到像这样的设备UUID

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

答案 3 :(得分:2)

使用identifierForVendor或advertisingIdentifier。

<强> 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.

<强> advertisingIdentifier:

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.

另外,请参阅Apple有关identifierForVendor和advertisingIdentifier的文档。

答案 4 :(得分:1)

在iOS 7中,Apple现在总是在查询MAC时返回固定值,以明确阻止 MAC作为ID 方案的基础。因此,您现在应该使用-[UIDevice identifierForVendor]或创建每个安装的UUID。

检查this SO问题。

答案 5 :(得分:0)

有2个解决方案:

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

答案 6 :(得分:0)

获取UDID:(如果您正在使用这个可能是App商店的人不会允许它 - &gt;根据我的关注)

- (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];

答案 7 :(得分:0)

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

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

答案 8 :(得分:0)

Swift 3.0 中获取 UUID

let UUIDValue = UIDevice.current.identifierForVendor!.uuidString

答案 9 :(得分:0)

在Swift 3.0中:

UIDevice.current.identifierForVendor!.uuidString

旧版

UIDevice.currentDevice().identifierForVendor

你想要一个字符串:

UIDevice.currentDevice().identifierForVendor!.UUIDString