Wednesday, 16 May 2018

Cross platform connectivity

For an application that requires data connectivity we must check if we are connect, keep track to see if our connectivity changes and maybe even identify if we are on a metered network. to accomplish all this we are going to leverage the "Xam.Plugin.Connectivity" nugget package.

once we have our nugget package we also have to ensure that our platforms request the adequate permissions.

  • Andriod
    • ACCESS_NETWORK_STATE
    • ACCESS_WIFI_STATE
  • Windows 10 UWP
    • Internet (Client)
    • Private Networks (Client & Server)
    • Internet (Client & Server)

To check if we have connectivity we can use the IsConnected property of the CrossConectivity singleton Current.

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using NetStatus.Views;
using Plugin.Connectivity;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace NetStatus 
{
    public partial class App : Application 
    {
        public App () 
        {
            InitializeComponent();
            var isConnected = CrossConnectivity.Current.IsConnected;
            MainPage = isConnected ? (Page)new NetworkViewPage() : new NoNetworkPage();
        }

        protected override void OnStart (){}
        protected override void OnSleep (){}
        protected override void OnResume (){}
    }
}

we created two pages the NetworkViewPage and the NoNetworkPage, their names are pretty self explanatory.

Next we need to handle a change in connectivity to do this we'll update OnStart Method

protected override void OnStart()
{
    CrossConnectivity.Current.ConnectivityChanged += (s, e) =>
    {
        Type currentPage = this.MainPage.GetType();
        if (e.IsConnected && currentPage != typeof(NetworkViewPage))
            this.MainPage = new NetworkViewPage();
        else if (!e.IsConnected && currentPage != typeof(NoNetworkPage))
            this.MainPage = new NoNetworkPage();
    };

}

what we did was created an event handler for when the connectivity changes, so that if we run our application and toggle the internet on and off we'll flip between our NetworkViewPage and our NoNetworkPage.

next let's look at our NetworkViewPage; here we'll update what type of connectivity we have should we switch betwen 3G, 4G or wifi

using Plugin.Connectivity;
using System.Linq; 
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace NetStatus.Views {
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NetworkViewPage : ContentPage {
public NetworkViewPage ()=>InitializeComponent ();
             
protected override void OnAppearing()
{
    base.OnAppearing();
    var connectionType = CrossConnectivity.Current.ConnectionTypes.First().ToString();

    ConnectionDetails_Label.Text = connectionType;

    CrossConnectivity.Current.ConnectivityChanged += (s, e) => {
        if (CrossConnectivity.Current.ConnectionTypes != null)
        {
            var connectionType = CrossConnectivity.Current.ConnectionTypes.FirstOrDefault();
            ConnectionDetails_Label.Text = connectionType.ToString();
        }
    };
}}}

the above code will let us know on page load what type of connectivity we are using and will update should it change.