Check if WIFI is available on Xamarin iOS

In Determine if a network is available , a class is introduced Reachability

. InternetConnectionStatus()

doesn't seem to do what I want. I want to check if WiFi is available, no matter if internet connection is possible. Don't know if the missing DNS server or something similar is the cause of this. Currently, it seems like the internet connection is always being checked. Is it the LocalWifiConnectionStatus()

best?

How can I only check if the user is logged in to the WLAN connection?

+3


source to share


4 answers


  • Open the Nuger Package Manager
  • Just search for the xam.plugin.connectivity plugin    and install it.

Now add this code to test internet access to your class and use namesapce using Plugin.Connectivity;



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

      

+4


source


I don't know if there have been any API updates since I wrote this code, but you can try reading the 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();
        }
    }

      

You can also try using LocalWifiConnectionStatus if that suits you better.



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

      

+1


source


Your question might be the answer when using Objective-C in

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

But code using Objective-C can easily port to xamarin C #. Check this link.

0


source


I know this is old, but I was looking for the same thing and combining the answers ... Does anyone have a better suggestion or a reason not to do it this way?

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

      

0


source







All Articles