The Network API provides events for monitoring network status changes, along with querying the current state of the network.
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;
let handler = Network.addListener('networkStatusChange', (status) => {
console.log("Network status changed", status);
});
// To stop listening:
// handler.remove();
// Get the current network status
let status = await Network.getStatus();
// Example output:
{
"connected": true,
"connectionType": "wifi"
}
The Network API requires the following permission be added to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This permission allows the app to access information about the current network, such as whether it is connected to wifi or cellular.
getStatus() => Promise<NetworkStatus>
Query the current network status
Returns:
Promise<NetworkStatus>
addListener(eventName: 'networkStatusChange', listenerFunc: (status: NetworkStatus) => void) => PluginListenerHandle
Listen for network status change events
Param | Type |
---|---|
eventName |
"networkStatusChange" |
listenerFunc |
(status: NetworkStatus) => void |
Returns:
PluginListenerHandle
removeAllListeners() => void
Remove all native listeners for this plugin
Prop | Type |
---|---|
connected |
boolean |
connectionType |
"none" | "unknown" | "wifi" | "cellular" |
Prop | Type |
---|---|
remove |
() => void |