views:

1321

answers:

4

I want to know the serial number of my iPhone using my application. I have writen code below.

- (NSString*)getSerialNumber
{
 CFTypeRef serialNumberAsCFString;

 io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));

 if (platformExpert)
 {
     serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
 }

 IOObjectRelease(platformExpert);

 NSString *serial = [[NSString alloc] initWithFormat:@"%@",serialNumberAsCFString];

    NSLog(@"serail no==>%@",serialNumberAsCFString);
    NSLog(@"serail no==>%@",serial);
}

Why am I still getting wrong serial number?

+3  A: 

Are you linking the IOKit framework?

Try the

id getValue(NSString *iosearch);

function, available at

http://blogs.oreilly.com/iphone/2008/08/retrieving-device-information.html

You can also use the UIDevice class to retrieve other useful information For instance, you can do:

NSString *id = [[UIDevice currentDevice] uniqueIdentifier];

Other useful properties are the following ones:

name
systemName
systemVersion
model
localizedModel

Kind regards

unforgiven
ok I have try this code , but my application is terminated improperly (pointer without cast)2) CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, iosearch, nil, kIORegistryIterateRecursively);( warning: passing argument 3 of 'IORegistryEntrySearchCFProperty' from incompatible pointer type)
+1  A: 

You should change the argument 2 of IORegistryEntryCreateCFProperty from "CFSTR (kIOPlatformUUIDKey)" to "CFSTR (kIOPlatformSerialNumberKey)". Then you will get the correct serial number(with length of 11 characters).

Dragon chi
A: 

Hi,

I am using this function - (NSString*)getSerialNumber to get the iphone serial number. when check for simulator its working but for device/iphone its not working giving multiple error.

Any idea.

thanks, Ajay

Ajay
A: 

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

0xced