2

I am trying to get Iphone's UDID on connect on a c# application. I've found that I could do that using IMobileDevice-net nuget package, But I could not find further documentation on how to use its classes.

Does anyone know where I can find documentation or have sample code?

Kaloy
  • 91
  • 1
  • 12
  • You mentioned iPhone and C#, I just want to clarify if this question pertains to using Xamarin? – Kevin Le - Khnle Feb 25 '20 at 19:46
  • No, just regular c# – Kaloy Feb 25 '20 at 19:47
  • What info do you know about device? I mean suppose there are multiple device connected then how will you like to get UDID of a particular one? – MKR Feb 25 '20 at 21:02
  • Yup that is current predicament i'm in right now, on how to differentiate between the connected UDID's. And the main purpose of this project is to get the device's information (IMEI, SerialNumber , etc) and finding out that device's UDID is the main starting point to doing that. – Kaloy Feb 26 '20 at 16:10

2 Answers2

4

The link to documentation of IMobileDevice-net seems broken. But you can find it at Documentation of IMobileDevice-net

Based on sample code provided for IMobileDevice-net one can find UDID of device if Name of the device is known. The code snippet for the same is:

ReadOnlyCollection<string> udids;
int count = 0;

var idevice = LibiMobileDevice.Instance.iDevice;
var lockdown = LibiMobileDevice.Instance.Lockdown;

// Get all devices connected
var ret = idevice.idevice_get_device_list(out udids, ref count);

if (ret == iDeviceError.NoDevice)
{
    // Not actually an error in our case
    return;
}

ret.ThrowOnError();

// Variable to store UDID of 
string foundUDID = "";
var nameOfDevice = "NameOfYourDevice";

// Get the device name
foreach (var udid in udids)
{
    iDeviceHandle deviceHandle;
    idevice.idevice_new(out deviceHandle, udid).ThrowOnError();

    LockdownClientHandle lockdownHandle;
    lockdown.lockdownd_client_new_with_handshake(deviceHandle, out lockdownHandle,
     "Quamotion").ThrowOnError();

    string deviceName;
    lockdown.lockdownd_get_device_name(lockdownHandle, out deviceName).ThrowOnError();


    deviceHandle.Dispose();
    lockdownHandle.Dispose();

    if(deviceName.equals(nameOfDevice))  //Check if name matches
    {
       foundUDID = udid;
       break;
    }

}
MKR
  • 19,739
  • 4
  • 23
  • 33
  • This definitely works, and I will try to make it work On connect of the device. Thanks! – Kaloy Feb 26 '20 at 16:13
  • I have tried implementing your solution with an On connect Handler, But I am not getting any response on the first connect of 1st device. It only gives me response when I connect a 2nd device which then returns the information of the first device connected – Kaloy Feb 27 '20 at 15:22
0

I'm now trying to implement the code above on Connect and I am using LibUsbDotnet DeviceNotify library for On connect events, but the issue now is the imobiledevice only returns info when I have two devices connected, it returns blank if only one device connected. On two device connected it shows the device #1 information.

here is my code

using iMobileDevice;
using iMobileDevice.iDevice;
using iMobileDevice.Lockdown;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using LibUsbDotNet.DeviceNotify;
using System.Windows.Forms;

namespace MobileDeviceDemo
{
    class Program
    {
        public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();
        static void Main(string[] args)
        {

            //// Hook the device notifier event
            UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;

            ////NativeLibraries.Load();



            // Exit on and key pressed.
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("Waiting for USB Devices connection");
            Console.Write("[Press any key to exit]");

            while (!Console.KeyAvailable)
                Application.DoEvents();

            UsbDeviceNotifier.Enabled = false;  // Disable the device notifier

            // Unhook the device notifier event
            UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
            //GenerateUDIDs();
            //Console.ReadLine();


        }

        private static void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
        {
            if (e.EventType.ToString() == "DeviceArrival")
            {
                Console.WriteLine("\n Device Connected");

                GenerateUDIDs();
            }

        }

        private static void GenerateUDIDs()
        {
            NativeLibraries.Load();

            ReadOnlyCollection<string> udids;
            int count = 0;

            var idevice = LibiMobileDevice.Instance.iDevice;
            var lockdown = LibiMobileDevice.Instance.Lockdown;

            var ret = idevice.idevice_get_device_list(out udids, ref count);

            if (ret == iDeviceError.NoDevice)
            {
                // Not actually an error in our case
                Console.WriteLine("No devices found");
                return;
            }

            ret.ThrowOnError();

            int NumberOfDeviceConnected = udids.Count;
            Console.WriteLine($"\n Number of Devices Connected: {NumberOfDeviceConnected}");

            int ctr = 0;
            // Get the device name
            foreach (var udid in udids)
            {
                ctr++;
                iDeviceHandle deviceHandle;
                idevice.idevice_new(out deviceHandle, udid).ThrowOnError();

                LockdownClientHandle lockdownHandle;
                lockdown.lockdownd_client_new_with_handshake(deviceHandle, out lockdownHandle, "Quamotion").ThrowOnError();

                string deviceName;
                lockdown.lockdownd_get_device_name(lockdownHandle, out deviceName).ThrowOnError();

                string sn;
                iMobileDevice.Plist.PlistHandle tested1;
                lockdown.lockdownd_get_value(lockdownHandle, null, "SerialNumber", out tested1).ThrowOnError();

                //Get string values from plist
                tested1.Api.Plist.plist_get_string_val(tested1, out sn);


                Console.WriteLine($"\n device: {ctr} Name: {deviceName}  UDID: {udid}  Serial Number: {sn}");

                deviceHandle.Dispose();
                lockdownHandle.Dispose();
            }
        }
    }
}

and the output is:

Waiting for USB Devices connection
[Press any key to exit]
 Device Connected

 Number of Devices Connected: 0  

   <!--- 1 Device Connected Not Displaying Output --->

 Device Connected

 Number of Devices Connected: 1

   <!--- 2 Device Connected Displaying Only the 1st device connected --->

 device: 1 Name: iPhone  UDID: 00008030-001538121A8A802E  Serial Number: F2LZR12AN70F

 Device Connected

 Number of Devices Connected: 2

   <!--- 3 Device Connected Displaying Only the 1st and 2nd device connected --->

 device: 1 Name: iPhone  UDID: 8fe1ee498514e1d98a8539c0f414ca5f611a8ea7  Serial Number: F4GY2NZRJC67

 device: 2 Name: iPhone  UDID: 00008030-001538121A8A802E  Serial Number: F2LZR12AN70F

Apparently iMobileDevice Code is not triggering response on the first connect, Any Help on this would greatly be appreciated

Kaloy
  • 91
  • 1
  • 12
  • and update guys, I have solved the problem I just have to have like a 3 - 5 second delay when the on connect event comes up – Kaloy Oct 02 '20 at 18:41