Getting the serial number (not UDID) of iphone programmatically

后端 未结 3 1393
礼貌的吻别
礼貌的吻别 2020-12-16 08:57

can anyone tell me the way for getting the serial number of an iPhone (not the UDID).

any immediate help will be appreciated..

相关标签:
3条回答
  • 2020-12-16 09:31

    Code example (this might be outdated) using a non-public API:

    http://www.iphonedevforums.com/forum/sdk-coding-help/145-unique-identifier-iphone.html

    @implementation AppLib
    ...
    
    - (NSString*)getSerialNumber
    {
        CFTypeRef serialNumberAsCFString;
        io_service_t platformExpert = IOServiceGetMatchingService(
            kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
        if (platformExpert)
            {
                serialNumberAsCFString = IORegistryEntryCreateCFProperty(
                    platformExpert, CFSTR(kIOPlatformSerialNumberKey), 
                    kCFAllocatorDefault, 0);
            }
        IOObjectRelease(platformExpert);
        NSString *serial = 
            [[NSString alloc] initWithFormat:@"%@",serialNumberAsCFString];
        return serial;
    }
    
    0 讨论(0)
  • 2020-12-16 09:32

    Ready to use category on UIDevice: UIDevice+serialNumber. Not sure this would be accepted on the App Store.

    0 讨论(0)
  • 2020-12-16 09:38

    Here i uploaded some code: But probably App store guys won't allow (As per my idea)

    #import <dlfcn.h>
    #import <mach/port.h>
    #import <mach/kern_return.h>
    
    @implementation ViewController
    
    - (NSString *) serialNumber
    {
    NSString *serialNumber = nil;
    
    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("IOPlatformSerialNumber"), kCFAllocatorDefault, 0);
                if (platformSerialNumber && CFGetTypeID(platformSerialNumber) == CFStringGetTypeID())
                {
                    serialNumber = [NSString stringWithString:(__bridge NSString *)platformSerialNumber];
                    CFRelease(platformSerialNumber);
                }
                IOObjectRelease(platformExpertDevice);
            }
        }
        dlclose(IOKit);
    }
        NSLog(@"Serial number::%@", serialNumber);
      return serialNumber;
     }
    
    0 讨论(0)
提交回复
热议问题