How To Check Network Connectivity In Xamarin.Forms

Introduction

Network connectivity refers to the internet connectivity of a device,  which can be either mobile data or Wi-Fi. In Xamarin.Forms, we are creating cross platform apps, so the different platforms have different implementations. In this blog, we will learn how to check internet connectivity in Xamarin.Forms apps.

Solution

To check the internet connection in Xamarin.Forms app, we need to follow the steps given below.

Step 1

Go to your solution and on your Portable project, right click and select Manage NuGet Packages.

cross

Step 2

Search and install Xam.Plugin.Connectivity NuGet Package.

cross

Step 3

Write the lines of code given below to check the internet connectivity.
  1. if (CrossConnectivity.Current.IsConnected) {  
  2.     // your logic...  
  3. else {  
  4.     // write your code if there is no Internet available  
  5. }  
Note

In Android platform, you have to allow the user permission to check internet connectivity. For this, use the steps given below.

Step 3(a)

Go to Droid Project, double click on properties and select Android Manifest option.

Step 3(b)

Now, select ACCESS_NETWORK_STATE and INTERNET permission. Save it and this permission will help the user to check the network state and access the internet.

cross

Thus, we have learnt how to check internet connectivity in Xamarin.Forms app.