问题描述
MonoTouch 是否有一个简单的机制来检索 iOS 设备的设备序列号(不是 UDID)?我可以使用第三方库来获取它吗?
Does MonoTouch have a simple mechanism for retrieving the device serial number (not UDID) of an iOS device? Is there a third-party library which I can use to obtain this?
以防万一,我希望在内部应用程序中使用此功能,而不关心 App Store 审批流程.
In case it matters, I'm looking to use this functionality in an in-house application and am not concerned with the App Store approval process.
推荐答案
更新:从 iOS 8 开始,我们无法检索 iDevice 的序列号.
要从 Monotouch 检索 iphone 序列号,您可以使用此技术:
To retrieve iphone serial number from Monotouch, you can use this technic:
- 从 XCode 创建一个静态库 .a,具有获取序列号的功能
- 在 MonoDevelop 中,创建一个绑定项目,将您的 .a 库绑定到 C# 类/函数(http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries)
- 在您的应用程序中,您调用此绑定库(在第 2 步中).
详情:
STEP 1. 在我的 library.a 中,我有一个类 DeviceInfo,这里是获取序列号的实现
STEP 1. In my library.a, I have a class DeviceInfo, here is the implementation to get Serial number
#import "DeviceInfo.h"
#import <dlfcn.h>
#import <mach/port.h>
#import <mach/kern_return.h>
@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 中我的绑定库项目的 ApiDefinition.cs 中,我添加了此绑定:
STEP 2. In ApiDefinition.cs of my Binding Library project in Monotouch, I add this binding:
[BaseType (typeof (NSObject))]
public interface DeviceInfo {
[Export ("serialNumber")]
NSString GetSerialNumber ();
}
第 3 步.在我的应用程序中,我在第 2 步中导入对绑定库项目的引用,然后添加
STEP 3. In my application, I import Reference to Binding library project in step 2, then add
使用 MyBindingProject;
using 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);
}
希望有所帮助.如果您有任何问题,请不要犹豫.
Hope that helps. Don't hesitate if you have any question.
这篇关于使用 MonoTouch 检索 iOS 设备的设备序列号的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!