【问题标题】:Get UDID of iphone on Connect c#在 Connect c# 上获取 iphone 的 UDID
【发布时间】:2020-06-09 14:58:15
【问题描述】:

我正在尝试在 c# 应用程序上连接时获取 Iphone 的 UDID。我发现我可以使用 IMobileDevice-net nuget 包来做到这一点,但我找不到关于如何使用它的类的更多文档。

有人知道我在哪里可以找到文档或有示例代码吗?

【问题讨论】:

  • 你提到了 iPhone 和 C#,我只是想澄清一下这个问题是否与使用 Xamarin 相关?
  • 不,只是普通的 c#
  • 您对设备了解多少?我的意思是假设连接了多个设备,那么您希望如何获取特定设备的 UDID?
  • 是的,这就是我现在所处的困境,关于如何区分连接的 UDID。而这个项目的主要目的是获取设备的信息(IMEI、SerialNumber 等)并找出设备的 UDID 是这样做的主要起点。

标签: c# windows libimobiledevice


【解决方案1】:

IMobileDevice-net 的文档链接似乎已损坏。但是你可以在Documentation of IMobileDevice-net找到它

根据为IMobileDevice-net 提供的示例代码,如果已知设备的Name,则可以找到设备的UDID。同样的代码sn-p是:

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

}

【讨论】:

  • 这绝对有效,我会尝试在连接设备时使其工作。谢谢!
  • 我已尝试使用 On connect 处理程序实施您的解决方案,但在第一次连接第一个设备时没有得到任何响应。它只在我连接第二个设备时给我响应,然后返回第一个连接的设备的信息
【解决方案2】:

我现在正在尝试在 Connect 上实现上面的代码,并且我正在使用 LibUsbDotnet DeviceNotify 库来处理 On connect 事件,但现在的问题是 imobiledevice 仅在我连接了两个设备时才返回信息,如果只有一个则返回空白连接的设备。 在连接的两台设备上,它显示设备#1 信息。

这是我的代码

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

输出是:

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

显然 iMobileDevice 代码在第一次连接时没有触发响应,对此的任何帮助将不胜感激

【讨论】:

  • 和更新伙计们,我已经解决了我只需要在 on connect 事件出现时延迟 3 - 5 秒的问题
猜你喜欢
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
相关资源
最近更新 更多