开发者

Getting the serial number (not UDID) of iphone programmatically

开发者 https://www.devze.com 2023-01-04 19:27 出处:网络
can anyone tell me the way for getting 开发者_开发问答the serial number of an iPhone (not the UDID).

can anyone tell me the way for getting 开发者_开发问答the serial number of an iPhone (not the UDID).

any immediate help will be appreciated..


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;
}


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


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

精彩评论

暂无评论...
验证码 换一张
取 消