获得设备的唯一标识符UDID
Posted 一米王子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获得设备的唯一标识符UDID相关的知识,希望对你有一定的参考价值。
在ios5之后,苹果为避免根据UDID获得用户的信息,而禁止使用uniqueIdentifier获得UDID,但是仍有些应用需要根据UDID区分设备
有一个系统的库IOKit.framework可以获得设备的唯一标识
NSString *serialNumber = nil; NSString * path = [[NSBundle mainBundle]pathForResource:@"IOKit.framework" ofType:nil]; const char * a =[path UTF8String]; // void *IOKit = dlopen(a, RTLD_NOW); void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW); if (IOKit) { mach_port_t *kIOMasterPortDefault = dlsym(IOKit, "kIOMasterPortDefault"); CFMutableDictionaryRef (*IOServiceMatching)(const char *name) = dlsym(IOKit, "IOServiceMatching"); mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, "IOServiceGetMatchingService"); CFTypeRef (*IORegistryEntryCreateCFProperty)(mach_port_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options) = dlsym(IOKit, "IORegistryEntryCreateCFProperty"); kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, "IOObjectRelease"); if (kIOMasterPortDefault && IOServiceGetMatchingService && IORegistryEntryCreateCFProperty && IOObjectRelease) { mach_port_t platformExpertDevice = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")); if (platformExpertDevice) { CFTypeRef platformSerialNumber= IORegistryEntryCreateCFProperty(platformExpertDevice, CFSTR("IOPlatformUUID"), kCFAllocatorDefault, 0); if (CFGetTypeID(platformSerialNumber) == CFStringGetTypeID()) { serialNumber = [NSString stringWithString:(__bridge NSString*)platformSerialNumber]; CFRelease(platformSerialNumber); } IOObjectRelease(platformExpertDevice); } } dlclose(IOKit); } return serialNumber;
但是这个方法只能获得模拟器上的UDID很坑爹啊
那还有另外的方法获得设备的信息,那就是走苹果的MDM
以上是关于获得设备的唯一标识符UDID的主要内容,如果未能解决你的问题,请参考以下文章
iOS获取设备唯一标识的各种方法?IDFAIDFVUDID分别是什么含义?
移动端设备标识码-DeviceID、IMEI、IDFA、UDID