简体   繁体   English

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

[英]How to get Device UDID in programmatically in iOS7?

How to get device UDID in programatically in iOS7. 如何在iOS7中以编程方式获取设备UDID。 [[UIDevice currentDevice] uniqueIdentifier] I was used this code This is deprecated iOS7. [[UIDevice currentDevice] uniqueIdentifier]我用过这段代码这是不推荐使用的iOS7。 how to get the device UDID. 如何获取设备UDID。 The UDID String changing when I delete the app and the reinstall means the UDID was getting different one. 删除应用程序时重新设置UDID字符串,重新安装意味着UDID变得不同。

It's work 100% to all the version 它对所有版本都是100%的工作

other best option recommend by apple use ASIdentifierManager Class Reference 苹果使用ASIdentifierManager类参考推荐的其他最佳选项

ios7-app-backward-compatible-with-ios5-regarding-unique-identifier ios7-APP-向后兼容与-关于唯一的ios5-标识符

this link tell you how to handle and use custom framework 此链接告诉您如何处理和使用自定义框架

uidevice-uniqueidentifier-property-is-deprecated-what-now 的UIDevice-唯一标识符,财产被弃用的-什么-现在

iOS 9 iOS 9

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

or 要么

it support only ios 6.0 and above 它仅支持ios 6.0及以上版本

code to use [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 代码使用[[[UIDevice currentDevice] identifierForVendor] UUIDString];

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

ios 5 to use like 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
}

UDID is no longer available in iOS 6+ due to security / privacy reasons. 出于安全/隐私原因,iOS 6+中不再提供UDID。 Instead, use identifierForVendor or advertisingIdentifier. 而是使用identifierForVendor或advertisingIdentifier。

Please go through this link. 请仔细阅读链接。

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

UPDATE for iOS 8+ 适用于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;
    }
}

In Swift you can get the device UUID like this 在Swift中你可以得到像这样的设备UUID

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

Use identifierForVendor or advertisingIdentifier. 使用identifierForVendor或advertisingIdentifier。

identifierForVendor: 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: 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.

Also, see Apple's documentation for the identifierForVendor and advertisingIdentifier. 另外,请参阅Apple的identifierForVendor和advertisingIdentifier文档。

In iOS 7, Apple now always returns a fixed value when querying the MAC to specifically thwart the MAC as base for an ID scheme. 在iOS 7中,Apple现在总是在查询MAC时返回固定值,以专门阻止MAC作为ID方案的基础。 So you now really should use -[UIDevice identifierForVendor] or create a per-install UUID. 所以你现在真的应该使用- [UIDevice identifierForVendor]或者创建一个每安装的UUID。

Check this SO Question. 检查这个 SO问题。

In Swift 3.0: 在Swift 3.0中:

UIDevice.current.identifierForVendor!.uuidString

old version 旧版本

UIDevice.currentDevice().identifierForVendor

you want a string: 你想要一个字符串:

UIDevice.currentDevice().identifierForVendor!.UUIDString

Have 2 solutions: 有2个解决方案:

  • You can use identifierForVendor after that store them to keychain and use later. 您可以使用identifierForVendor将它们存储到钥匙串并稍后使用。 Because value of keychain will not changed when re-install app. 因为重新安装应用程序时钥匙串的价值不会改变。
  • You can try OpenUDID 你可以试试OpenUDID

For getting UDID: (If you're using this Might be App store guys won't allow it --> As per my concern) 获得UDID :(如果你使用这个可能是App商店的人不会允许它 - >根据我的关注)

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

For Getting UUID : 获取UUID

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

Swift 2.2+ proper way to get UUID: Swift 2.2+获取UUID的正确方法:

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

Swift 3.0中获取UUID

let UUIDValue = UIDevice.current.identifierForVendor!.uuidString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM