Internet connection detection in WinRT

 

After a small hiatus during summer, back to track!

It is very common to have to know if the device your app is running has internet access and/or if that connection is cut/resumed.

WinRT has a nice class, NetworkInformation , that help us dealing with those topics (and even more)

If you want to know if you have an internet access, just call the GetConnectionProfiles method and check if the result is not null :

public static bool IsConnected
        {
            get
            {
                return NetworkInformation.GetInternetConnectionProfile() != null;
            }
        }

It is nice…but not enough. We’d like to know also if there is a connection change.

Hopefully, there is an event for that : NetworkStatusChanged

(Note : It didn’t detect the cut of the internet connection pre-RTM versions of Windows 8, but it works fine now)

With that, we can create a small helper class that will warn us where there is a connection change :

using System;
using Windows.Networking.Connectivity;

namespace InternetDetection
{
    public static class InternetTools
    {
        public delegate void InternetConnectionChangedHandler(object sender, InternetConnectionChangedEventArgs args);

        public static event InternetConnectionChangedHandler InternetConnectionChanged;

        static InternetTools()
        {
            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
        }

        private static void NetworkInformation_NetworkStatusChanged(object sender)
        {
            var arg = new InternetConnectionChangedEventArgs
                          {IsConnected = (NetworkInformation.GetInternetConnectionProfile() != null)};

            if (InternetConnectionChanged != null)
                InternetConnectionChanged(null, arg);
        }

        public static bool IsConnected
        {
            get
            {
                return NetworkInformation.GetInternetConnectionProfile() != null;
            }
        }
    }

    public class InternetConnectionChangedEventArgs : EventArgs
    {
        public bool IsConnected { get; set; }
    }

    
}

An example :

using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Navigation;

namespace InternetDetection
{
    /// 
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// 
    public sealed partial class MainPage
    {
        public MainPage()
        {
            this.InitializeComponent();

            ShowConnectionStatus(InternetTools.IsConnected);

            InternetTools.InternetConnectionChanged += (sender, args) => ShowConnectionStatus(args.IsConnected);
        }

        private void ShowConnectionStatus(bool connectionstatus)
        {
            if (connectionstatus)
            {
               // Connected 
            }
            else
            {
               // Not connected
            }
        }


    }
}

 

Nice and easy !

You can find the source code with a sample app here.

The following two tabs change content below.
Olivier

Olivier

Mobile Engineer at Arcana Studio
Freelance developer. Passionate for mobile Development and IoT. Expert in WinRT and Xamarin. MVP Windows Platform Development Nokia Developer Champion