【问题标题】:What is the simplest way to retrieve the device serial number of an iOS device using MonoTouch?使用 MonoTouch 检索 iOS 设备的设备序列号的最简单方法是什么?
【发布时间】:2015-01-21 18:15:09
【问题描述】:

MonoTouch 是否有一种简单的机制来检索 iOS 设备的设备序列号(不是 UDID)?是否有第三方库可供我获取?

以防万一,我希望在内部应用程序中使用此功能,并且不关心 App Store 的审批流程。

【问题讨论】:

  • 我正在专门寻找基于 MonoTouch 的解决方案,并认为应该有一种比编写和绑定原生 iOS 库以获得单一功能更简单的方法。
  • 这是一个私有的 C API。如果您可以链接 IOKit(可能与 dlopen() 动态链接?)并从 MonoTouch 调用 C 函数(我的印象是 C# 可以毫不费力地做到这一点),这应该相当容易,否则您需要编写一些本机代码或找到其他人。此外,IME 对象文件的兼容性通常会在主要的 iOS SDK 更新中中断,因此需要将此类库作为源代码或作为经常更新的二进制文件提供;对于无法在应用商店上架的功能,后者似乎不值得。
  • 是否可以使用不同的 ID?还是您特别需要 UUID?您可以使用其中一些(例如广告 ID developer.apple.com/library/ios/#documentation/AdSupport/…)来代替,并且更受 Xamarin 和 Apple 的支持。

标签: ios xamarin.ios serial-number


【解决方案1】:

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

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

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

详情:

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

#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 中我的 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);
            }

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

【讨论】:

  • 我尝试了代码并在 iOS7_beta 上运行良好。顺便说一句,我不确定它是否会在不久的将来被弃用,或者 DLL IOKit.Framework 的可访问性会受到影响?如果能找到,请回复有效链接。
  • 谢谢,我试过了,但它在 iOS 8 beta 5 中崩溃了……还有其他适用于 iOS 7 和 iOS 8 的好方法吗?请指教
  • 不行,在iOS8中无法获取序列号。
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多