2

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

any immediate help will be appreciated..

user347161
  • 327
  • 1
  • 4
  • 14

3 Answers3

4

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;
}
miku
  • 181,842
  • 47
  • 306
  • 310
  • 4
    I'd note here that it uses IOKit, which is not public, so using it in your app will get it rejected from the AppStore – unbeli Jun 28 '10 at 13:21
  • 1
    thank you Roque... your comments were very helpful... My idea was to use serial number(short and convenient) instead of UDID(lengthy) of iPhone in my app ..But i have no choices left – user347161 Jun 28 '10 at 13:30
  • A minor but important point: According to the Memory Management Guidelines, the string returned from this method should be autoreleased. The method name does not suggest to the caller that they are taking ownership of the returned object (doesn't contain init, copy, new etc). This will likely lead to a memory leak as the caller is unlikely to release the returned object. – mttrb Apr 21 '12 at 05:17
1

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;
 }
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
1

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

0xced
  • 25,219
  • 10
  • 103
  • 255
  • 1
    Thanks, I tried this but it gives crash in iOS 8 beta 5....any other good approach for both iOS 7 and iOS 8?? Please advise – Meet Aug 13 '14 at 06:09