Wednesday 9 January 2019

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.





Step 2

Search and install Xam.Plugin.Connectivity NuGet Package.




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.



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

11 comments:

  1. Loved your efforts on it buddy. Thanks for sharing this with us
    Vmware training
    Vmware certification
    Vmware course

    ReplyDelete

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2