14 KiB
id, title, original_id
| id | title | original_id |
|---|---|---|
| version-0.49-netinfo | netinfo | netinfo |
NetInfo #
NetInfo exposes info about online/offline status
ConnectionType enum #
ConnectionType describes the type of connection the device is using to communicate with the network.
Cross platform values for ConnectionType:
none- device is offlinewifi- device is online and connected via wifi, or is the iOS simulatorcellular- device is connected via Edge, 3G, WiMax, or LTEunknown- error case and the network status is unknownAndroid-only values for
ConnectionType:bluetooth- device is connected via Bluetoothethernet- device is connected via Ethernetwimax- device is connected via WiMAXEffectiveConnectionType enum #
Cross platform values for
EffectiveConnectionType:2g3g4gunknownAndroid #
To request network info, you need to add the following line to your app's
AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />isConnectionExpensive #
Available on Android. Detect if the current active connection is metered or not. A network is classified as metered when the user is sensitive to heavy data usage on that connection due to monetary costs, data limitations or battery/performance issues.
NetInfo.isConnectionExpensive() .then(isConnectionExpensive => { console.log('Connection is ' + (isConnectionExpensive ? 'Expensive' : 'Not Expensive')); }) .catch(error => { console.error(error); });isConnected #
Available on all platforms. Asynchronously fetch a boolean to determine internet connectivity.
NetInfo.isConnected.fetch().then(isConnected => { console.log('First, is ' + (isConnected ? 'online' : 'offline')); }); function handleFirstConnectivityChange(isConnected) { console.log('Then, is ' + (isConnected ? 'online' : 'offline')); NetInfo.isConnected.removeEventListener( 'change', handleFirstConnectivityChange ); } NetInfo.isConnected.addEventListener( 'change', handleFirstConnectivityChange );Connectivity Types (deprecated) #
The following connectivity types are deprecated. They're used by the deprecated APIs
fetchand thechangeevent.iOS connectivity types (deprecated):
none- device is offlinewifi- device is online and connected via wifi, or is the iOS simulatorcell- device is connected via Edge, 3G, WiMax, or LTEunknown- error case and the network status is unknownAndroid connectivity types (deprecated).
NONE- device is offlineBLUETOOTH- The Bluetooth data connection.DUMMY- Dummy data connection.ETHERNET- The Ethernet data connection.MOBILE- The Mobile data connection.MOBILE_DUN- A DUN-specific Mobile data connection.MOBILE_HIPRI- A High Priority Mobile data connection.MOBILE_MMS- An MMS-specific Mobile data connection.MOBILE_SUPL- A SUPL-specific Mobile data connection.VPN- A virtual network using one or more native bearers. Requires API Level 21WIFI- The WIFI data connection.WIMAX- The WiMAX data connection.UNKNOWN- Unknown data connection.The rest of the connectivity types are hidden by the Android API, but can be used if necessary.
Methods #
static addEventListener(eventName, handler) #
Adds an event handler. Supported events:
connectionChange: Fires when the network status changes. The argument to the event handler is an object with keys:type: AConnectionType(listed above)effectiveType: AnEffectiveConnectionType(listed above)
change: This event is deprecated. Listen toconnectionChangeinstead. Fires when the network status changes. The argument to the event handler is one of the deprecated connectivity types listed above.
static removeEventListener(eventName, handler) #
Removes the listener for network status changes.
static fetch() #
This function is deprecated. Use getConnectionInfo instead. Returns a promise that
resolves with one of the deprecated connectivity types listed above.
static getConnectionInfo() #
Returns a promise that resolves to an object with type and effectiveType keys
whose values are a ConnectionType and an EffectiveConnectionType, (described above),
respectively.
static isConnectionExpensive() #
Properties #
isConnected: ObjectExpression #
An object with the same methods as above but the listener receives a boolean which represents the internet connectivity. Use this if you are only interested with whether the device has internet connectivity.
Improve this page by sending a pull request!