What is the simplest way to retrieve the device serial number of an iOS device using MonoTouch?

OpenAway picture OpenAway · Feb 23, 2013 · Viewed 8.7k times · Source

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?

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.

Answer

Duyen-Hoa picture Duyen-Hoa · Mar 5, 2013

UPDATE: from iOS 8, we cannot retrieve the serial number of our iDevice.

To retrieve iphone serial number from Monotouch, you can use this technic:

  1. Create a static library .a from XCode that have a function to get serial number
  2. In MonoDevelop, create a binding project to bind you .a library into C# classes/functions (http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries)
  3. In your application, you call this binding library (in step 2).

For detail:

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

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

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

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.