メインコンテンツまでスキップ
バージョン: v2

Network

The Network API provides events for monitoring network status changes, along with querying the current state of the network.

Example

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"
}

Android Note

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.

API

getStatus()

getStatus() => Promise<NetworkStatus>

Query the current network status

Returns:

Promise<NetworkStatus>


addListener(...)

addListener(eventName: 'networkStatusChange', listenerFunc: (status: NetworkStatus) => void) => PluginListenerHandle

Listen for network status change events

ParamType
eventName"networkStatusChange"
listenerFunc
(status: NetworkStatus) => void

Returns:

PluginListenerHandle


removeAllListeners()

removeAllListeners() => void

Remove all native listeners for this plugin


Interfaces

NetworkStatus

PropType
connectedboolean
connectionType"none" | "unknown" | "wifi" | "cellular"

PluginListenerHandle

PropType
remove() => void