检索iOS设备标识符。

5

有没有办法在iOS SDK中获取iOS设备标识符?我想要访问Xcode在组织者-设备部分显示的标识符,类似于:21xb1fxef5x2052xec31x3xd3x48ex5e437xe593。


你为什么想要获取那个标识符? - Nick Bull
2
我需要唯一标识设备。 - aumanets
6个回答

12

看起来在iOS 6下仍然可以访问UDID,但是自从iOS 5.0起已经被标记为不推荐使用,因此你不应该使用它(无论如何你都会收到关于此的警告)

[UIDevice currentDevice].uniqueIdentifier

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

[UIDevice currentDevice].identifierForVendor

或者如果它与某种广告相关,则:

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

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

我想如果你想支持iOS < 6的话,可以采取类似以下的方法:

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

但我不确定苹果在审核过程中会如何做出回应。

您还可以使用一些第三方解决方案,例如openUDIDsecureUDID。开放式和安全的UDID已被弃用-请使用供应商/广告标识符。


更新

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

自iOS7以来,MAC地址不再可用。(可以读取它,但它将始终是相同的虚拟地址02:00:00:00:00:00)。


该属性在iOS 5.0中已被弃用。 - aumanets
是的,抱歉,一开始我写成了uniqueIdentifier而不是identifierForVendor,但无论如何,当我写下你不应该使用uniqueIdentifier并在第二行写下你应该使用它时,我认为很明显出现了问题。 - lupatus
SecureUDID不再受Crashlytics支持;他们写道:“我们建议使用苹果的iOS 6供应商和广告标识符。”。 - Andreas Ley
谢谢,我已经更新了我的答案,并加入了MAC地址,这是自iOS7以来不可用的。 - lupatus

7

来自苹果文档

一串字母数字组合,基于各种硬件细节独特地标识每个设备。(只读)(在iOS 5.0中已弃用。请改用此类的identifierForVendor属性或ASIdentifierManager类的advertisingIdentifier属性,或使用NSUUID类的UUID方法创建UUID并将其写入用户默认数据库。)


2
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"];
  }
}

0

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

[[UIDevice currentDevice] uniqueIdentifier]

0
+ (NSString *)uuid
{
    NSString *uuidString = nil;
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    if (uuid) {
        uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
    }
    return [uuidString autorelease];
}

它百分之百有效,甚至在模拟器上也是如此...


-1

有的。

[[UIDevice currentDevice] uniqueIdentifier]

编辑: 然而在iOS 5中这已被弃用。该标识符不再应在iOS 5中使用。阅读这个 SO帖子以获取更多细节。


3
这在iOS 5中已被弃用,使用此功能的应用将无法进入App Store。 - Tom van der Woerdt

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