ConnectivityPlugin

Connectivity Plugin for Xamarin and Windows


Project maintained by jamesmontemagno Hosted on GitHub Pages — Theme by mattgraham

Checking Connectivity

There are a few properties that can be used to easily check connection information using the plugin.

Check Connectivity

IsConnected: The easiest and most common use case of simply checking if there is internet access:

/// <summary>
/// Gets if there is an active internet connection
/// </summary>
bool IsConnected { get; }

Example:

public async Task<string> MakeWebRequest()
{
    if(!CrossConnectivity.Current.IsConnected)
    {
      //You are offline, notify the user
      return null;
    }

    //Make web request here
}

Check Type of Connection

Easily check what type of internet connection is currently active.

/// <summary>
/// Gets the list of all active connection types.
/// </summary>
IEnumerable<ConnectionType> ConnectionTypes { get; }

Example:

public async Task<string> MakeWebRequestWifiOnly()
{
    var wifi = Plugin.Connectivity.Abstractions.ConnectionType.WiFi;
    var connectionTypes = CrossConnectivity.Current.ConnectionTypes;
    if(!connectionTypes.Contains(wifi))
    {
      //You do not have wifi
      return null;
    }

    //Make web request here
}

Speed of Connection

You can query all bandwidths of the active connections in Bits Per Second.

/// <summary>
/// Retrieves a list of available bandwidths for the platform.
/// Only active connections.
/// </summary>
IEnumerable<UInt64> Bandwidths { get; }

Example:

public async Task<string> MakeWebRequestOneMeg()
{
    var optimalSpeed = 1000000; //1Mbps
    var speeds = CrossConnectivity.Current.Bandwidths;

    //If on iOS or none were returned
    if(speeds.Length == 0)
      return null;

    if(!connectionTypes.Any(speed => speed > optimalSpeed))
    {
      //You do not have wifi
      return null;
    }

    //Make web request here
}

Platform Tweaks:

<= Back to Table of Contents