I am trying programatically to detect ethernet adapter cable when connected to iPhone in Xamarin Forms but till now whatever I tried is not working on iOS device. Communication is happening between iPhone and device when I connect ethernet cable from device to iPhone using Lightning to ethernet adapter but can’t detect when cable connects to iPhone.
Is it possible to achieve this using Xamarin.Forms?
I tried these:
Solution 1:
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
{
return true;
}
}
This is giving true always whether device is connected to ethernet adapter or not.
Solution 2:
using Xamarin.Essentials
if (Connectivity.ConnectionProfiles.Contains(ConnectionProfile.Ethernet)){
return true
}
This works for android but not for iOS
Solution 3:
var monitor = new NWPathMonitor();
var queue = new CoreFoundation.DispatchQueue("NetworkMonitor");
monitor.SetQueue(queue);
var isEthernetConnected = false;
monitor.SetUpdatedSnapshotHandler((path) =>
{
path.EnumerateInterfaces((ni) =>
{
if(ni.InterfaceType == NWInterfaceType.Wired)
isEthernetConnected = true;
});
});
monitor.Start();
return isEthernetConnected;
when connected to ethernet count of interface is zero so it’s also not working in Xamarin iOS platform specific.