@napacitor/connectivity
Network status detection and monitoring plugin wrapping the .NET MAUI Connectivity API.
Installation
To use npm:
npm install @napacitor/connectivity
To use yarn:
yarn add @napacitor/connectivity
Sync native files:
npx nap sync
Usage
import { Connectivity } from '@napacitor/connectivity';
// Get the current network status
const status = await Connectivity.getStatus();
console.log(status.connectionType, status.networkAccess, status.isConnected);
// Check for internet access
const hasInternet = await Connectivity.hasInternetAccess();
// Listen for network status changes
const handle = await Connectivity.addListener('connectivityChange', (status) => {
console.log('Network changed:', status.connectionType, status.isConnected);
});
await handle.remove();
await Connectivity.removeAllListeners();
How It Works
Wraps the .NET MAUI Connectivity.Current facade: NetworkAccess maps the access level, ConnectionProfiles maps the connection type (priority Ethernet > WiFi > Cellular > Bluetooth, an empty set is NONE and anything else is UNKNOWN). The native ConnectivityChanged event is subscribed when the first JS listener registers and unsubscribed when the last one is removed. Note: a platform reporting Internet only guarantees a network connection exists, not actual reachability (e.g. Wi-Fi connected but the router is offline).
Permissions
Android
The ACCESS_NETWORK_STATE permission must be declared (required by the MAUI documentation). Add it to AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Or add an assembly attribute in Platforms/Android/MainApplication.cs:
[assembly: UsesPermission(Android.Manifest.Permission.AccessNetworkState)]
iOS
No additional configuration required.
Windows
No additional configuration required.
API
getStatus()
Gets the current network status.
Returns: Promise<NetworkStatus>
hasInternetAccess()
Checks for internet access (only true when NetworkAccess is Internet).
Returns: Promise<boolean>
addListener('connectivityChange', callback)
Listens for network status changes. The native event is subscribed when the first listener registers and unsubscribed when the last one is removed.
| Param | Type |
|---|---|
eventName | 'connectivityChange' |
callback | ConnectivityChangeCallback |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
removeAllListeners()
Removes all listeners of this plugin.
Returns: Promise<void>
Interfaces
NetworkStatus
| Prop | Type | Description |
|---|---|---|
connectionType | ConnectionType | Connection type: NONE / WIFI / CELLULAR / ETHERNET / BLUETOOTH / UNKNOWN. |
networkAccess | NetworkAccess | Access level: INTERNET / CONSTRAINED_INTERNET / LOCAL / NONE / UNKNOWN. |
isConnected | boolean | Whether there is a network (convenience field, true when NetworkAccess is not NONE). |