Saturday, February 27, 2016

UWP : How to check for Internet connectivity in a UWP Application

 Check for Internet connectivity in a UWP Application


Last Updated on : 28th February 2016

Hey guys !! Hope you are doing well. 

At times there are situations when you want your app to behave or respond to  Internet connectivity changes. If that is what you are looking for in case of UWP (Universal Windows Platform), you are at the right place.

Just like you, I was developing a UWP application and I got into a situation where detecting the changes in Internet connectivity and responding accordingly was crucial. Hence I started searching around and the solution wasn't too far. The class which comes to the rescue and does it all for you is :

Windows.Networking.Connectivity.NetworkInformation

I decided to play a little and hence made a simple POC(Proof of Concept) called InternetPOC that you can download and use. I have hosed in on Google Drive and you can find the link at the bottom of the page. Having said that, let us go through the steps on how to use the above class for our Internet connectivity check. All it takes are the following two steps:

Step 1 : Register the event handler for NetworkStatusChanged.
Step 2 : Write appropriate code in the handler to respond to change in Internet connectivity.

In my case, I have decided to show a dialog saying "Internet is present" when Internet connectivity is there and saying "Internet is not present" when internet is gone. I installed the app on my Windows Phone and tried turning my Wi-Fi on and off.and the results were as expected.

Case 1 : Internet is present


Case 2 : Internet is not present


Now let us go through the implementation. This is my MainPage.xaml.cs class where all the trick resides.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Networking.Connectivity;
using Windows.UI.Popups;
using System.Threading.Tasks;


namespace InternetPOC
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            // Registering the handler for NetworkStatusChanged
            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
            CheckInternetAndShowDialog();
        }

        /// <summary>
        /// NetworkStatusChangeHandler : Implementaion of what to do when Internet Connectivity changes 
        /// </summary>
        /// <param name="sender"></param>
        private void NetworkInformation_NetworkStatusChanged(object sender)
        {
            // This is what happens when Network Status Changes 
            CheckInternetAndShowDialog();
        }

        private void CheckInternetAndShowDialog()
        {
            Task.Run(async () =>
            {
                var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                bool HasInternetAccess = (connectionProfile != null &&
                                     connectionProfile.GetNetworkConnectivityLevel() ==
                                     NetworkConnectivityLevel.InternetAccess);
                if (HasInternetAccess)
                {
                    MessageDialog md = new MessageDialog("Internet is present");
                    await md.ShowAsync();
                }
                else
                {
                    MessageDialog md = new MessageDialog("Internet is not present");
                    await md.ShowAsync();
                }
            });
        }
    }
}


You can download the full project from the following link (Hosted on Google Drive):


No comments:

Post a Comment

Thanks for your valuable comment