0

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?

DerStarkeBaer
  • 669
  • 8
  • 28
  • 1
    From within an app on the device or from computer connected to the device via USB? Does your app have to pass app store approval or can it use private APIs? – Paulw11 Sep 01 '17 at 07:47
  • No the app will be uploaded to the App store. My app need an ID of every device that login with the app to the server... – DerStarkeBaer Sep 01 '17 at 08:05
  • You can use `identifierForVendor`. You can't use IMEI for an App Store app. Not all devices have an IMEI anyway; iPods and non-cellular iPads for example – Paulw11 Sep 01 '17 at 08:21

3 Answers3

1

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

vassilag
  • 450
  • 8
  • 18
1

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;
        }
    }
DerStarkeBaer
  • 669
  • 8
  • 28
0

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;
    }
}
Tam Tran
  • 1
  • 1