3

MonoTouchには、iOSデバイスのデバイスシリアル番号(UDIDではない)を取得するための簡単なメカニズムがありますか?これを取得するために使用できるサードパーティのライブラリはありますか?

重要な場合は、この機能を社内アプリケーションで使用することを検討しており、AppStoreの承認プロセスには関与していません。

4

1 に答える 1

8

更新:iOS 8から、iDeviceのシリアル番号を取得できません。

Monotouchからiphoneのシリアル番号を取得するには、次の手法を使用できます。

  1. シリアル番号を取得する機能を持つ静的ライブラリ.aをXCodeから作成します
  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のBindingLibraryプロジェクトのApiDefinition.csに、次のバインディングを追加します。

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

ステップ3.私のアプリケーションでは、ステップ2でBindingライブラリプロジェクトへの参照をインポートしてから、

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

お役に立てば幸いです。ご不明な点がございましたら、お気軽にお問い合わせください。

于 2013-03-05T12:57:33.013 に答える