A library that provides access to the device's network such as its IP address, MAC address, and airplane mode status.
expo-network
provides useful information about the device's network such as its IP address, MAC address, and airplane mode status.
-
npx expo install expo-network
If you are installing this in an existing React Native app, start by installing expo
in your project. Then, follow the additional instructions as mentioned by the library's README under "Installation in bare React Native projects" section.
On Android, this module requires permissions to access the network and Wi-Fi state. The permissions ACCESS_NETWORK_STATE
and ACCESS_WIFI_STATE
are added automatically.
import * as Network from 'expo-network';
Network.getIpAddressAsync()
Gets the device's current IPv4 address. Returns 0.0.0.0
if the IP address could not be retrieved.
On web, this method uses the third-party ipify service
to get the
public IP address of the current device.
Promise<string>
A Promise
that fulfils with a string
of the current IP address of the device's main
network interface. Can only be IPv4 address.
Example
await Network.getIpAddressAsync();
// "92.168.32.44"
Network.getNetworkStateAsync()
Gets the device's current network connection state.
On web, navigator.connection.type
is not available on browsers. So if there is an active
network connection, the field type
returns NetworkStateType.UNKNOWN
. Otherwise, it returns
NetworkStateType.NONE
.
A Promise
that fulfils with a NetworkState
object.
Example
await Network.getNetworkStateAsync();
// {
// type: NetworkStateType.CELLULAR,
// isConnected: true,
// isInternetReachable: true,
// }
Network.isAirplaneModeEnabledAsync()
Tells if the device is in airplane mode.
Promise<boolean>
Returns a Promise
that fulfils with a boolean
value for whether the device is in
airplane mode or not.
Example
await Network.isAirplaneModeEnabledAsync();
// false
NetworkState
Name | Type | Description |
---|---|---|
isConnected (optional) | boolean | If there is an active network connection. Note that this does not mean that internet is reachable.
This field is |
isInternetReachable (optional) | boolean | If the internet is reachable with the currently active network connection. On Android, this
depends on |
type (optional) | NetworkStateType | A |
CELLULAR
NetworkStateType.CELLULAR = "CELLULAR"
Active network connection over mobile data or DUN-specific
mobile connection when setting an upstream connection for tethering.
Code | Description |
---|---|
ERR_NETWORK_IP_ADDRESS | On Android, there may be an unknown Wi-Fi host when trying to access WifiManager in getIpAddressAsync . On iOS, no network interfaces could be retrieved. |
ERR_NETWORK_UNDEFINED_INTERFACE | An undefined interfaceName was passed as an argument in getMacAddressAsync . |
ERR_NETWORK_SOCKET_EXCEPTION | An error was encountered in creating or accessing the socket in getMacAddressAsync . |
ERR_NETWORK_INVALID_PERMISSION_INTERNET | There are invalid permissions for android.permission.ACCESS_WIFI_STATE in getMacAddressAsync . |
ERR_NETWORK_NO_ACCESS_NETWORKINFO | Unable to access network information |