# Detect network connectivity

{% hint style="warning" %}
This section is relevant for building [Wisej.NET Hybrid **local** applications](/hybrid/development/local-application.md) 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.&#x20;
{% endhint %}

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? <a href="#why-detect-network-connectivity-on-mobile-applications" id="why-detect-network-connectivity-on-mobile-applications"></a>

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:

![Screenshot of an app showing a warning about limited network connectivity.](https://learn.microsoft.com/en-us/training/dot-net-maui/consume-rest-services-maui/media/2-internet-error.png)

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 <a href="#detect-network-connectivity" id="detect-network-connectivity"></a>

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:

```csharp
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.

```csharp
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.

{% embed url="<https://learn.microsoft.com/en-us/training/modules/consume-rest-services-maui/2-detect-network-connectivity>" %}
Source Material
{% endembed %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/hybrid/rest-web-services/detect-network-connectivity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
