How to verify internet access in Universal Apps

Recently I faced a scenario where I needed to know if the user had internet connection or not. The application is running on Windows 8.1 and Windows Phone 8.1 and is built as a Universal App where I share all the code and only have platform specific UI.

The solution to this problem wasn’t obvious at a start, a quick search on Bing didn’t give much information at first. At last I found a few posts on Stackoverflow that had a few ideas how to check for internet.

One interesting solution was to open an HttpClient and just try to go to some random site, e.g Bing.com. If this was success you could assume we have internet connection, and if it failed we could assume that we don’t have access. But as a consultant at Jayway this solution feels really awkward and if you have to repeatedly check for access, it would drain you mobile data.

But out of the box there is a really nice class: Windows.Networking.Connectivity.NetworkInformation. With this class you can easily get information if you got internet access or not.

Method to use to check for internet:

When implemented this method you could use it everywhere when you need to check for internet, and it works both on Windows 8.1 and Windows Phone 8.1. I placed the code in a PCL but you could place it in the shared project or directly in the Windows 8.1 or Windows Phone 8.1 project if you like.

Worth noting is that MSDN writes the following about the method:

The GetNetworkConnectivityLevel method may take time in some cases to determine the current value of the network connectivity level. The recommended process for determining the network connectivity level is to register a handler for the NetworkStatusChanged event on the NetworkInformation class. When a notification is received of a network status change, obtain the new connectivity level by calling the GetNetworkConnectivityLevel method on the profile returned by the GetInternetConnectionProfile method. The returned network connectivity level can then be stored for later use when needed. This also ensures that the correct ConnectionProfile is checked.

So if you plan to use it frequently throughout the app, add a listener on NetworkStatusChanged and when the status changes, get the NetworkConnectivityLevel and store it so it can easily be returned when someone wants to check the internet access.

All you need to do is to create an instance of the connection class somewhere and then run:

 

14 Comments

  1. Hi there, just became aware of your blog through Google, and
    found that it is truly informative. I am gonna watch out for
    brussels. I will appreciate if you continue this in future.
    A lot of people will be benefited from your writing.
    Cheers!

  2. Don Heuvelman

    Thank you sir!
    This blog helped me on my way!

  3. Hi, I have tried your method, it works but in some situation it doesn’t work as expected.

    When the device is connected to router, but router is disconnected from the internet. So it means application will be kind of offline.

    Firstly, it won’t fire status change event, and then the connectivity level is still 3 which is connected to internet.

    Thanks.

  4. Hey I have also noticed if they are connected to a router and windows does not notice the limited access the application does not notice it. I am assuming this is because the app is talking to windows and not actually doing anything to check. Are we left with just a try catch on the client for this use case then ?

  5. nuri yılmaz

    it is look like solution. but ctor is not static but property has modified by static. it will never work if someting has reference one instance of Connection.
    and tell me about unit testing?

  6. Alin N.

    Hello,

    I am using your method but i have an issue when running my app on Windows 10 Emulator.
    The GetNetworkConnectivityLevel() method returns LocalAccess instead of InternetAccess.

    Do you know why this thing happens ?

    PS: i have internet access on my emulator, opened the browser and checked if it works.
    Thank you !

  7. Eli Black

    Looks helpful. Thanks!

  8. Brent

    This code is returning false for both win 10 desktop and win phone 10 . Any ideas why?

  9. Aditya Sharma

    Well in UWP I got all false too. I modified the code as follows:

    public static bool HasInternetAccess { get { return CheckInternetAccess(); } }

    public Connection()
    {
    NetworkInformation.NetworkStatusChanged += NetworkInformationOnNetworkStatusChanged;
    CheckInternetAccess();
    }

    private void NetworkInformationOnNetworkStatusChanged(object sender)
    {
    CheckInternetAccess();
    }

    private static bool CheckInternetAccess()
    {
    var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
    var HasInternetAccess = (connectionProfile != null &&
    connectionProfile.GetNetworkConnectivityLevel() ==
    NetworkConnectivityLevel.InternetAccess);
    return HasInternetAccess;
    }

    The method calling remains the same and each time you want to check the internet availability check via the If(Connection.HasInternetAccess) logic. I am not sure if the delegates would still work but you won’t need delegated you could check each time you want to make sure internet connectivity. I looped in a timer to check each 15 mins to check for internet availability

  10. Anand Kishore

    I ‘m not able to find any method as named GetInternetConnectionProfile() in the NetworkInformation Class.

Trackbacks for this post

  1. Andrey on .NET | Интересности #57

Leave a Reply