首页 > 代码库 > 获得设备的唯一标识符UDID

获得设备的唯一标识符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

详情参考http://www.cnblogs.com/liyy2015/p/6030032.html

获得设备的唯一标识符UDID