检查Xamarin iOS上是否有WIFI可用

时间:2014-10-14 06:45:35

标签: c# ios xamarin.ios xamarin wifi

Detect if Network is Available中引入了Reachability类。 InternetConnectionStatus()似乎没有按我的意愿行事。我想检查WiFi是否可用,无论是否可以连接互联网。不知道是否是丢失的DNS服务器或类似的原因。目前,似乎始终测试Internet连接。 LocalWifiConnectionStatus()是更好的吗?

如何仅检查用户是否已登录WLAN连接?

4 个答案:

答案 0 :(得分:4)

  1. 打开Nuger包管理器
  2. 只需搜索插件即可      xam.plugin.connectivity 并安装它。
  3. 现在添加此代码以检查您的课程的互联网,并使用名称使用Plugin.Connectivity;

    private bool CheckConnectivity()
        {
            var isConnected = CrossConnectivity.Current.IsConnected;
            return isConnected;
        }
    

答案 1 :(得分:1)

我不知道自从我编写此代码以来是否有API更新,但您可以尝试阅读SSID:

    /// <summary>
    /// Gets the current SSID.
    /// </summary>
    /// <value>The current SSID.</value>
    public string CurrentSSID 
    {
        get
        {
            NSDictionary dict;
            var status = CaptiveNetwork.TryCopyCurrentNetworkInfo ("en0", out dict);
            if (status == StatusCode.NoKey)
            {
                return string.Empty;
            }

            var bssid = dict [CaptiveNetwork.NetworkInfoKeyBSSID];
            var ssid = dict [CaptiveNetwork.NetworkInfoKeySSID];
            var ssiddata = dict [CaptiveNetwork.NetworkInfoKeySSIDData];

            return ssid.ToString();
        }
    }

如果能够更好地满足您的需求,您还可以尝试使用LocalWifiConnectionStatus。

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/src/Xamarin.Forms.Labs/Xamarin.Forms.Labs.iOS/Services/Reachability.cs

    /// <summary>
    /// The local WiFi connection status.
    /// </summary>
    /// <returns>
    /// The <see cref="NetworkStatus"/>.
    /// </returns>
    public static NetworkStatus LocalWifiConnectionStatus()
    {
        NetworkReachabilityFlags flags;
        return (!IsAdHocWiFiNetworkAvailable(out flags) || (flags & NetworkReachabilityFlags.IsDirect) == 0) ?
            NetworkStatus.NotReachable :
            NetworkStatus.ReachableViaWiFiNetwork;
    }

答案 2 :(得分:0)

我知道这已经过时了,但我正在寻找同样的事情并将各种答案结合起来......任何人都有更好的建议或理由不这样做?

public NetworkState GetNetworkState()
{
    object ssid = null;
    NSDictionary dict = null;

    var status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);
    if (status == StatusCode.OK)
        ssid = dict[CaptiveNetwork.NetworkInfoKeySSID];

    using (SystemConfiguration.NetworkReachability r = new NetworkReachability("www.appleiphonecell.com"))
    {
        NetworkReachabilityFlags flags;

        if (r.TryGetFlags(out flags))
        {
            if ((flags & NetworkReachabilityFlags.Reachable) != 0)
            {
                if (ssid != null && !string.IsNullOrEmpty(ssid.ToString()))
                    return NetworkState.Wifi;
                else
                    return NetworkState.Carrier;//we are not connected to wifi, but we can reach the site...
            }
        }
    }

    return NetworkState.None;
}

答案 3 :(得分:-1)

如果在

使用Objective-C,您的问题可以回答

How to check for local Wi-Fi (not just cellular connection) using iPhone SDK?

但是使用Objective-C的代码可以很容易地移植到xamarin c#。请查看此链接