Xamarin, Xamarin Essentials

Working with connectivity in Xamarin Forms

I think you have been in a situation that you lost internet connection and you stay without knowing what happened since nothing notified you  ?‍♀. .. And it is in bad taste since as users we practically do not know what is happening and we simply believe that the application had stopped working ?. To avoid this, as Developers, is very important to handle the connection states of our applications since we can make decisions based on them about what to show or do in our App when a certain state is notified and the most important: If we can find out about these changes we can keep our users informed about what’s happening with their connectivity making their experience more pleasant!  ?

So in this article we will be learning how do it using one of the great APIs that Xamarin Essentials bring to us! This explanation will be divided into the following points:

➖Platform settings

➖Using network categories

➖Profiles connections


Platform settings

This image has an empty alt attribute; its file name is if_android_313085.png

To start to need to get access to the The AccessNetworkState permission, to do it you just have to add a specific implementation for Android. (Other platforms don’t need it).

To implementate it you have three different options:

First option: In your “Properties” folder locate the AssemblyInfo.cs file and add the following line of code under the permissions already added by default:

Second option: In your AndroidManifest.xml add the following code:

Third option: Make right click in your Android project ➡ Click in Options ➡ Android Applications ➡ Them you will see Required permissions and find AccessNetworkState and check it!

Let’s start!

Xamarin Essential bring to us the Connectivity class which allow us to know changes in the device’s network conditions.

We can get the following network states:

This image has an empty alt attribute; its file name is Screen-Shot-2020-06-09-at-2.14.03-PM-1024x687.png

So, to get the current state of the network access you just have to do the following:

And then,  you can verify the state of the connection using any of the five categories mentioned above:

It’s important to note that it is possible that Internet is reported by NetworkAccess but full access to the web is not available. Due to how connectivity works on each platform it can only guarantee that a connection is available.

Also you can check what type of connection profile the device is actively using. This is very important since the result can help you make decisions about what to do or not in your App.

You can check the connection profile options in the following image:

This image has an empty alt attribute; its file name is Screen-Shot-2020-06-08-at-10.00.37-PM-1024x471.png

To use it, you just have to use this code:

⚠ Don’t forget to add using System.Linq; to be able to use Contains without problems! ?

Handling events

Finally, when the connection profile or network access changes you can handle it with an events!

? Some tips:

? Always try to keep the user visually informed about their connection status, this will help you understand that there are no problems in the Application because it did not do a certain action. (UI/UX).  I leave you this link so you can take ideas!

?  Profile connections are very important because thanks to these you can identify and make decisions to do or not do some action in your App. For example: I would like that my App only download files when I have Wifi activated.

? Manage cached data for when there is no connection your user stays happy!  (For example: Using a local database like Realm or SQLite or using  caching library )

From now on you can manage your connections!

Spanish post: https://medium.com/@reyes.leomaris/trabajando-con-conectividad-en-xamarin-forms-bf7e02ef0546

Thanks for reading!!! ??



Tagged , , ,

3 thoughts on “Working with connectivity in Xamarin Forms

  1. And how can i know if the movil is connected to internet, but it dont have access to the web, to browse and consult data on the cloud?.

Leave a Reply

Your email address will not be published. Required fields are marked *