NetworkInterface.GetAllNetworkInterfaces() returns interfaces with OperationalStatus of 'Unknown' in Xamarin.iOS

StackOverflow https://stackoverflow.com/questions/17868420

Question

I am writing an iOS app using Xamarin in Xamarin Studio on a MAC. I am using an open source uPNP implementation. In it, they use NetworkInterface.GetAllNetworkInterfaces().

My problem is that each of the returned interfaces has its OperationalStatus property set to 'Unknown'.

In Windows, all of these are set to 'Up' or 'Down'. However, when using Xamarin and running this same code on an iOS device, all of the interfaces have an OperationalStatus of 'Unknown'.

This is resulting in all of them being discarded because it is expecting at least one to be 'Up'. So, is there some method or something I need to do to get an accurate OperationStatus on these interfaces?

I am trying to do some cross-platform development and don't want to special case the uPNP code if at all possible.

Thanks for your assistance !!!

Était-ce utile?

La solution

Per Xamarin Support:

Looks like I have some bad news: http://forums.xamarin.com/discussion/1706/system-net-networkinformation-getallnetworkinterfaces-fails It's a known bug that hasn't been addressed. You will have to make native calls to find out the network interface iOS:
http://docs.xamarin.com/recipes/ios/network/reachability/detect_if_network_is_available For this kind of device specific code, you can look at using Compiler Directives #if / #endif Building Cross Platform Applications - check out part 3 and part 5 for practical setups. you can also check our sample apps for how to configure your projects.

So here's what I ended up doing:

    try
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

        foreach (var netInterface in interfaces)
        {
            if ((netInterface.OperationalStatus == OperationalStatus.Up ||
                 netInterface.OperationalStatus == OperationalStatus.Unknown) &&
                (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
             netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
            {
                foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
                {
                    if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        var ipAddress = addrInfo.Address;
                        CurrentAddressTable.Add(ipAddress);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Helper.Debug(ex.ToString());
    }

Since Xamarin's implementation does not set the NetworkInterface.OperationalStatus, I am forced to allow either 'Up' or 'Unknown' to be accepted. Although this approach works on Windows and on iOS, it does not work on Android devices. It is frustrating that such a simple thing as getting a valid Local IP address should be so difficult. But I guess this is the way things are at this point.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top