如何以编程方式获取iPhone的序列号?

如何以编程方式获取iPhone的序列号?,iphone,objective-c,Iphone,Objective C,我想知道使用我的应用程序的iPhone的序列号。我在下面写了代码 - (NSString*)getSerialNumber { CFTypeRef serialNumberAsCFString; io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")); if (platformExpert)

我想知道使用我的应用程序的iPhone的序列号。我在下面写了代码

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

为什么我仍然得到错误的序列号?

您正在链接IOKit框架吗

试试

id getValue(NSString *iosearch);
功能,可在

您还可以使用UIDevice类来检索其他有用的信息 例如,您可以执行以下操作:

NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
其他有用的属性如下所示:

  name  
  systemName    
  systemVersion  
  model  
  localizedModel  

您应该将
IORegistryEntryCreateCFProperty
的参数2从
CFSTR(kIOPlatformUUIDKey)
更改为
CFSTR(kIOPlatformSerialNumberKey)
。然后,您将获得正确的序列号(长度为11个字符)。

UIDevice上的Ready to use类别:。不确定App Store是否接受此选项。

你说的“序列号错误”是什么意思?它没有显示,或者只是不正确?您需要序列号或UUID吗?Apple docs说UUID在所有设备中都是唯一的,您应该使用这样的编号,而不是设备序列号。Apple Store接受吗?好的,我尝试了这个代码,但我的应用程序被错误终止,并在3个地方给我警告:1)CFTypeId propID=NULL;(不带强制转换的指针)2)CFTypeRef prop=IORegistryEntrySearchCFProperty(entry,kIODeviceTreePlane,iosearch,nil,kioregistryiterate递归);(警告:从不兼容的指针类型传递“IORegistryEntrySearchCFProperty”的参数3)