Detect network connectivity

This section is relevant for building Wisej.NET Hybrid local applications that are running locally on the device. It is not possible to detect network connectivity changed events when using a Wisej.NET Hybrid remote application that runs on a remote web server.

Mobile devices use Wi-Fi and cellular technologies to connect to the internet. This dependency means that your users could lose their internet connection while using your application. If you don't add code to protect against this possibility, your app could stop responding and provide your users with a bad experience.

Why detect network connectivity on mobile applications?

Detecting if you have an Internet connection on a mobile application is important because mobile devices can frequently lose their connection. This may be due to poor coverage by a network service provider, or being in an environment that has limited or no reception, such as a tunnel, deep valley, or high mountain. There are also different types of network connectivity. If you're located in an environment that provides WiFi connectivity, you typically have higher bandwidth than if you're dependent on cellular access. You might still be able to connect to the Internet, but some operations, such as streaming video content, might be slower (and expensive) over a cellular link compared to a WiFi connection.

Because mobile devices have these challenges, you must write code to protect against them. If you don't, and your application attempts to perform operations that use the internet, your application might stop responding.

You also want to provide a good user experience when your application can't connect to the Internet. If your application stops working because there's no Internet service, your users might be left confused. The best thing to do is to provide information to your users. Tell them that you don't have an Internet connection and that your application might not perform fully without it. The following image shows an example:

In this example, the application developer informs the user that they don't have an internet connection and they should attempt to connect to Wi-Fi.

Detect network connectivity

To check for network connectivity in a Wisej.NET Hybrid app, use the Connectivity class. This class exposes a property called NetworkAccess and an event named ConnectivityChanged. You can use these members to detect changes in the network.

You access the NetworkAccess property through the Device gateway singleton called Device.Info.Networking.

The NetworkAccess property returns a value from the NetworkAccess enumeration. The enumeration has five values: ConstrainedInternet, Internet, Local, None, and Unknown. If the NetworkAccess property returns a value of NetworkAccess.None, then you know you don't have a connection to the Internet, and you shouldn't run networking code. This mechanism is portable across platforms. The following code shows an example:

if (Device.Info.Networking.NetworkAccess == NetworkAccess.None)
{
    ...
}

The ConnectivityChanged event also allows you to determine if the device is connected to the Internet. The ConnectivityChanged event is triggered automatically when the network status changes. For example, if you start with an active network connection and eventually lose it, the ConnectivityChanged event is raised to inform you about the change.

Device.Info.Networking.ConnectivityChanged += Connectivity_ConnectivityChanged;
...
void Connectivity_ConnectivityChanged(object sender, EventArgs e)
{
    bool connected = Device.Info.Networking.NetworkAccess == NetworkAccess.Internet;
}

The ConnectivityChanged event allows you to write apps that can detect a change in network status and seamlessly adjust the functionality available according to the different environments.

Last updated