Detect if Network is Available

PDF for offline use
Related Samples:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to detect the type of network connection that is in use.

Recipe

The Reachability sample provides a static class named Reachability that can be used to determine network status. Add a copy of this class to your project, and then use the static methods it provides.

  • To see if a web site is available you can check like this:
if(!Reachability.IsHostReachable("http://google.com")) {
    // Put alternative content/message here
}
else
{
    // Put Internet Required Code here
}
  • To check what type of internet connection is available, use the static method Reachability.InternetConnectionStatus():
NetworkStatus internetStatus = Reachability.InternetConnectionStatus();

Reachability.InternetConnectionStatus returns the enum NetworkStatus which provides some information about the network connection that will be used for internet connectivity:

public enum NetworkStatus
{
       NotReachable,
       ReachableViaCarrierDataNetwork,
       ReachableViaWiFiNetwork
}

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.