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:
➖Using network categories
To start to need to get access to the The
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!
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:
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:
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! ?
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!
Thanks for reading!!!