gpt4 book ai didi

ios - NetworkInterface.GetAllNetworkInterfaces() 返回 Xamarin.iOS 中 OperationalStatus 为 'Unknown' 的接口(interface)

转载 作者:行者123 更新时间:2023-11-29 03:44:48 26 4
gpt4 key购买 nike

我正在 MAC 上的 Xamarin Studio 中使用 Xamarin 编写 iOS 应用程序。我正在使用开源 uPNP 实现。其中,他们使用 NetworkInterface.GetAllNetworkInterfaces()。

我的问题是每个返回的接口(interface)都将其 OperationalStatus 属性设置为“未知”。

在 Windows 中,所有这些都设置为“向上”或“向下”。但是,当使用 Xamarin 并在 iOS 设备上运行相同的代码时,所有接口(interface)的 OperationalStatus 均为“未知”。

这会导致所有这些都被丢弃,因为它预计至少有一个处于“Up”状态。那么,我需要采取某种方法或措施来获取这些接口(interface)上的准确 OperationStatus 吗?

我正在尝试进行一些跨平台开发,如果可能的话,不想对 uPNP 代码进行特殊处理。

感谢您的帮助!!!

最佳答案

根据 Xamarin 支持:

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.

这就是我最终所做的:

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

由于 Xamarin 的实现未设置 NetworkInterface.OperationalStatus,因此我被迫允许接受“Up”或“Unknown”。尽管此方法适用于 Windows 和 iOS,但不适用于 Android 设备。令人沮丧的是,像获取有效的本地 IP 地址这样简单的事情竟然如此困难。但我想这就是目前的情况。

关于ios - NetworkInterface.GetAllNetworkInterfaces() 返回 Xamarin.iOS 中 OperationalStatus 为 'Unknown' 的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com