使用MonoTouch检索iOS设备的设备序列号的最简单方法是什么?

MonoTouch有一个简单的机制来检索iOS设备的设备序列号(而不是UDID)吗? 我可以使用第三方库来获取此信息吗?

如果它很重要,我希望在内部应用程序中使用此function,而不关心App Store批准过程。

更新:从iOS 8,我们无法检索我们的iDevice的序列号。

要从Monotouch中检索iphone序列号,您可以使用以下技术:

  1. 从XCode创建一个静态库.a,它具有获取序列号的function
  2. 在MonoDevelop中,创建一个绑定项目,将您.a库绑定到C#classes / functions( http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries
  3. 在您的应用程序中,您调用此绑定库(在步骤2中)。

详情如下:

步骤1.在我的library.a中,我有一个类DeviceInfo,这里是获取序列号的实现

 #import "DeviceInfo.h" #import  #import  #import  @implementation DeviceInfo - (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 (CFGetTypeID(platformSerialNumber) == CFStringGetTypeID()) { serialNumber = [NSString stringWithString:(__bridge NSString*)platformSerialNumber]; CFRelease(platformSerialNumber); } IOObjectRelease(platformExpertDevice); } } dlclose(IOKit); } return serialNumber; } @end 

步骤2.在Monotouch中我的Binding Library项目的ApiDefinition.cs中,我添加了这个绑定:

 [BaseType (typeof (NSObject))] public interface DeviceInfo { [Export ("serialNumber")] NSString GetSerialNumber (); } 

步骤3.在我的应用程序中,我在步骤2中导入引用到绑定库项目,然后添加

使用MyBindingProject;

 string serialNumber = ""; try { DeviceInfo nativeDeviceInfo = new DeviceInfo (); NSString temp = nativeDeviceInfo.GetSerialNumber(); serialNumber = temp.ToString(); } catch (Exception ex) { Console.WriteLine("Cannot get serial number {0} - {1}",ex.Message, ex.StackTrace); } 

希望有所帮助。 如果您有任何疑问,请不要犹豫。