如何在iOS7中以编程方式获取设备的UDID?

28

如何在iOS7中以编程方式获取设备UDID。[[UIDevice currentDevice] uniqueIdentifier]我曾经使用过这段代码,但它已经被废弃了。如何获取设备UDID呢?当我删除并重新安装应用程序时,UDID字符串会更改,这意味着获得了不同的UDID。


可能是 UIDevice uniqueIdentifier Deprecated - What To Do Now? 的重复问题。 - Simon
只有给声望,才能对他人有用,并且声望会赢得更多的声望。 - codercat
10个回答

34

对于所有版本,它都能够百分百地工作。

苹果推荐的另一个最佳选择是使用ASIdentifierManager Class Reference

iOS7中应用程序关于唯一标识符向后兼容iOS5

此链接告诉您如何处理和使用自定义框架。

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

iOS9

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"]; 缺少 alloc。 - Vladimír Slavík
我在iOS 9上进行测试,却得到了'没有已知的initWithUUIDString选择器的类方法'的错误。为什么会这样呢? - Jayprakash Dubey
从字符串(例如“E621E1F8-C36C-495A-93FC-0C247A3E6E5F”)创建NSUUID。对于无效的字符串返回nil。@JayprakashDubey - codercat
你可以使用以下代码创建一个UUID:alloc NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"20B0DDE7-6087-4607-842A-E97C72E4D522"]; @JayprakashDubey - codercat
如果在ASIdentifierManager中不提供广告服务,您是否会被AppStore拒绝? - Nick
@codercat - 我的评论是一个问题。你是说如果不提供广告,它就会被拒绝吗? - Nick

20

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

请访问此链接了解更多信息。

   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;
    }
}

4
是的,我之前使用了上述代码。但是当我删除并重新安装我的应用程序时,会生成不同的 UDID,它将动态更改。 - iSara
以上示例未能提供正确的设备UDID值,而且该值在不同系统中会发生变化。 - Alok
我相信这是苹果的意图。如果用户有删除应用程序的理由,那么他们就不应该再被跟踪了。 - Allison
2
别担心,我也有解决方案。使用Keychain... :) - Tapas Pal

6

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

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

1
他在询问UDID,而不是UUID。 - jxmorris12

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.

广告标识符:

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和advertisingIdentifier的文档。

1
在Swift 3.0中:
UIDevice.current.identifierForVendor!.uuidString

old version

UIDevice.currentDevice().identifierForVendor

你想要一个字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString

1
在iOS 7中,苹果现在总是返回一个固定值来查询MAC地址,以特别阻止将MAC地址作为ID的方案。因此,您现在应该使用{{link1: - [UIDevice identifierForVendor]}}或创建每次安装的UUID。
请查看{{link2:this}} SO问题。

我曾使用这段代码[UIDevice identifierForVendor],但当我运行我的应用程序时,它会返回一个ID,然后删除并再次运行我的应用程序,它将返回不同的ID。它会生成随机ID。 - iSara

0

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

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

0
在Swift 3.0中获取UUID的方法是:

UUID

let UUIDValue = UIDevice.current.identifierForVendor!.uuidString

0

获取UDID:(如果您正在使用此方法,可能会被应用商店拒绝-->据我所知)

- (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 代码现在不起作用了。你能确认一下吗?我尝试使用 Xcode 8.2.1 和 iOS 10.2 设备。 - DShah

0

有两种解决方案:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接