What Is the Simplest Way to Retrieve the Device Serial Number of an iOS Device Using Monotouch

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

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.

Xamarin iOS Platform - Get Device Serial# Method - iOS 8 or Later

The link you post in the question is out of date and Apple does not allow you to get device serial number right now.

You can have a try to use identifierForVendor as the unique device id.

We had come across one solution like creating a static library from
XCode with Serial Number implementation. Then create the object c
static library and binding this to Xamarin.iOS project.

I would like to say that if you find a solution in native iOS, then you can do it in Xamarin.iOS.

You don't need to create a objective-c static library and binding this to Xamarin.iOS project.

How to get device serial number of an iOS using Xamarin forms

That information is private to the user and will not be accessible.
If you need to identify a user in some way, you can use the IdentifierForVendor property - that's a unique identifier for a device based on a AppStore developer.

var id = UIDevice.CurrentDevice.IdentifierForVendor;

iOS get serial number of device (iPad) on iOS7

Here is a working solution from 0xced (thanks to him!):

UIDevice+serialNumber category

Just tried it and it works like a charm, but as he pointed (and you said it's OK), won't be accepted by AppStore.

How to detect iOS device type Xamarin

Use this to detect device type.


if(Device.RuntimePlatform == Device.iOS)
{
//what is the device type
if (Device.Idiom == TargetIdiom.Phone)
{
Console.WriteLine(Device.Idiom);
}
else if(Device.Idiom == TargetIdiom.Tablet)
{
Console.WriteLine(Device.Idiom);
}

}
else if(Device.RuntimePlatform == Device.Android)
{
//what is the device type
}



Related Topics



Leave a reply



Submit