Thursday, 8 December 2016

Get Serial Number of Device

Get Serial Number of Device



Write Following Code in ViewController.m :-

#import <dlfcn.h>
#import <mach/port.h>
#import <mach/kern_return.h>





- (void)viewDidLoad {
    [super viewDidLoad];
    

    NSString *str = [self serialNumber];

    NSLog(@"Serial Number : %@"str) ;
  
}


- (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);
    }
    
    return serialNumber;
}





4 comments:

  1. it is not working in ios 12 device. it returns null always

    ReplyDelete
  2. Interesting one, i saw your article in google, very interesting to read.I have seen this blog,its really nice to read and has more informative information.

    Flutter App | flutter programmierer | flutter entwicklung | flutter entwickler | Flutter app Entwickler

    ReplyDelete
  3. Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts into the stuff you post!!

    Flutter | Flutter App | flutter programmierer | flutter entwicklung | flutter entwickler | Flutter app Entwickler

    ReplyDelete
  4. Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts into the stuff you post!!

    Flutter | Flutter App | flutter programmierer | flutter entwicklung | flutter entwickler | Flutter app Entwickler

    ReplyDelete