有没有办法在iOS SDK中获取iOS设备标识符?我想要访问Xcode在组织者-设备部分显示的标识符,类似于:21xb1fxef5x2052xec31x3xd3x48ex5e437xe593。
有没有办法在iOS SDK中获取iOS设备标识符?我想要访问Xcode在组织者-设备部分显示的标识符,类似于:21xb1fxef5x2052xec31x3xd3x48ex5e437xe593。
看起来在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;
}
但我不确定苹果在审核过程中会如何做出回应。
您还可以使用一些第三方解决方案,例如openUDID或secureUDID。开放式和安全的UDID已被弃用-请使用供应商/广告标识符。
更新
另一种可能性是使用MAC地址作为唯一哈希的基础,例如您可以使用来自ODIN1的代码-源代码在这里
自iOS7以来,MAC地址不再可用。(可以读取它,但它将始终是相同的虚拟地址02:00:00:00:00:00)。
来自苹果文档:
一串字母数字组合,基于各种硬件细节独特地标识每个设备。(只读)(在iOS 5.0中已弃用。请改用此类的identifierForVendor属性或ASIdentifierManager类的advertisingIdentifier属性,或使用NSUUID类的UUID方法创建UUID并将其写入用户默认数据库。)
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"];
}
}
您可以找到唯一的设备标识符:
[[UIDevice currentDevice] uniqueIdentifier]
+ (NSString *)uuid
{
NSString *uuidString = nil;
CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid) {
uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}
return [uuidString autorelease];
}
它百分之百有效,甚至在模拟器上也是如此...
有的。
[[UIDevice currentDevice] uniqueIdentifier]
编辑: 然而在iOS 5中这已被弃用。该标识符不再应在iOS 5中使用。阅读这个 SO帖子以获取更多细节。