How to get IMEI from IOS device using C#

3.6k views Asked by At

I need to read out the IMEI of an IOS device using C#...

Is this even possible in C#/Xamarin? Or is there another value that i can use to identify a device?

3

There are 3 answers

1
vassilag On BEST ANSWER

Some device identifiers are now impossible to be obtained from public APIs of iOS:

IMSI - International Mobile Subscriber Identity (SIM card number)

IMEI - International Mobile Equipment Identity (Device ID)

UDID - Unique Device Identifier for Apple iDevices

MAC address - Media Access Control Address (Network address)

Take a look here: http://studyswift.blogspot.gr/2015/12/asidentifiermanager-get-idfv-vendor.html

If you could use any of the provided IDs the code is in Swift but if you use C# / Xamarin it won't be difficult to convert.

Hope this helps

0
Pedro Henrique Pozzer On

I've also tried to find a way to capture the IMEI, but I believe this is not possible. The only way I solved it was to use this code, it returns serial number

 public class IosDevice 
 {
        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern uint IOServiceGetMatchingService(uint masterPort, IntPtr matching);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern IntPtr IOServiceMatching(string s);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern IntPtr IORegistryEntryCreateCFProperty(uint entry, IntPtr key, IntPtr allocator, uint options);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern int IOObjectRelease(uint o);

        public string GetIdentifier()
        {
            string serial = string.Empty;
            uint platformExpert = IOServiceGetMatchingService(0, IOServiceMatching("IOPlatformExpertDevice"));

            if (platformExpert != 0)
            {
                NSString key = (NSString)"IOPlatformSerialNumber";
                IntPtr serialNumber = IORegistryEntryCreateCFProperty(platformExpert, key.Handle, IntPtr.Zero, 0);

                if (serialNumber != IntPtr.Zero)
                {
                    serial = NSString.FromHandle(serialNumber);
                }

                IOObjectRelease(platformExpert);
            }

            return serial;
        }
    }
0
Tam Tran On

In case someone wants to get vid, pid of an USB Device in MacOS

public class OsxDeviceDiscovery
{
    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOServiceGetMatchingService(int masterPort, IntPtr matching);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOServiceGetMatchingServices(int masterPort, IntPtr matching, out IntPtr iterator);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern IntPtr IOServiceMatching(string name);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern IntPtr IORegistryEntryCreateCFProperty(int entry, IntPtr key, IntPtr allocator, uint options);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOObjectRelease(int o);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOIteratorNext(IntPtr iterator);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern bool CFNumberGetValue(IntPtr number,long type, ref long value);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int CFNumberGetType(IntPtr number);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern bool CFStringGetCString(IntPtr stringRef, byte[] str, int size, int encoding);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IORegisterEntryCreateIterator(IntPtr entry, IntPtr plane, int options, out IntPtr iterator);

    public static string GetIdentifier()
    {
        string deviceName = string.Empty;
        IntPtr matchingNodes;
        int platformExpert = IOServiceGetMatchingServices(0, IOServiceMatching("IOUSBDevice"), out matchingNodes);

        int node = -1;
        while ((node = IOIteratorNext(matchingNodes)) != 0)
        {
            long vendorID = 0;
            long productID = 0;
            long locationId = 0;

            NSString key = (NSString)"idVendor";
            IntPtr proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref vendorID);
            }

            key = (NSString)"idProduct";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref productID);
            }

            if (vendorID != 0x1234 || productID != 0x5678)
            {
                IOObjectRelease(node);
                continue;
            }

            key = (NSString)"locationID";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref locationId);
            }

            key = (NSString)"kUSBSerialNumberString";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                byte[] byteArray = new byte[20];
                CFStringGetCString(proRef, byteArray, 20, 0x0600);
                string serialNumber = System.Text.Encoding.UTF8.GetString(byteArray);
                deviceName = "/dev/cu.usbmodem" + serialNumber;
            }


            IOObjectRelease(node);
        }

        return deviceName;
    }
}