Checking for Connectivity the Bulletproof Way

A huge amount of the currently released apps in the Store does a check upon app start up to see if connectivity is available. If this is not the case, then the user is redirected to an offline page, and there is no way to continue use of the app.

This is a major concern, when several of the apps does not correctly check for connectivity. One such case, is when the user connects to a VPN. In that case the main connection will be changed to ‘Limited’ even though the user still has an active connection to the internet.

The below snippet will check all the connection profiles for internet access:

public static bool IsConnected
{
get
{
var profiles = NetworkInformation.GetConnectionProfiles();
var internetProfile = NetworkInformation.GetInternetConnectionProfile();
return profiles.Any(s => s.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
|| (internetProfile != null
&& internetProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
}
}

The naive way to check for internet access would be: NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel()== NetworkConnectivityLevel.InternetAccess , but this approach fails in the scenario described above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s