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

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

本文介绍了使用MonoTouch检索iOS设备的设备序列号的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:


  1. 从XCode创建一个静态库.a,它具有获取序列号的功能

  2. 在MonoDevelop中,创建一个绑定项目来绑定你.a库到C#类/函数()

  3. 在您的应用程序中,您调用此绑定库(在步骤2中)。






对于细节l:


For detail:

步骤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.在ApiDefinition中。在Monotouch中我的Binding Library项目的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.在我的应用程序中,我在步骤中导入对Binding库项目的引用2,然后使用MyBindingProject添加

STEP 3. In my application, I import Reference to Binding library project in step 2, then add

;

...

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设备的设备序列号的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:46