Expo 网络
一个提供访问设备网络信息的库,例如其 IP 地址、MAC 地址和飞行模式状态。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-network 提供了有关设备网络的有用信息,例如其 IP 地址、MAC 地址以及飞行模式状态。
安装
- npx expo install expo-networkIf you are installing this in an existing React Native app, make sure to install expo in your project.
配置
在 Android 上,此模块需要权限才能访问网络和 Wi-Fi 状态。权限 ACCESS_NETWORK_STATE 和 ACCESS_WIFI_STATE 会自动添加。
API
import * as Network from 'expo-network';
Hooks
Returns the current network state of the device. This method initiates a listener for network state changes and cleans up before unmounting.
NetworkStateThe current network state of the device, including connectivity and type.
Example
const networkState = useNetworkState(); console.log(`Current network type: ${networkState.type}`);
Methods
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"
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.
Promise<NetworkState>A Promise that fulfils with a NetworkState object.
Example
await Network.getNetworkStateAsync(); // { // type: NetworkStateType.CELLULAR, // isConnected: true, // isInternetReachable: true, // }
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
Event Subscriptions
| Parameter | Type | Description |
|---|---|---|
| listener | (event: NetworkState) => void | Callback to execute when the network state changes. The callback is provided with a single argument that is an object containing information about the network state. |
Adds a listener that will fire whenever the network state changes.
EventSubscriptionA subscription object with a remove function to unregister the listener.
Example
const subscription = addNetworkStateListener(({ type, isConnected, isInternetReachable }) => { console.log(`Network type: ${type}, Connected: ${isConnected}, Internet Reachable: ${isInternetReachable}`); });
Types
| Property | 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 |
Type: NetworkState
Represents an event that provides the updated network state when there is a change in the network status.
This is passed as the argument to listeners registered with addNetworkStateListener().
Enums
An enum of the different types of devices supported by Expo.
NetworkStateType.CELLULAR = "CELLULAR"Active network connection over mobile data or DUN-specific
mobile connection when setting an upstream connection for tethering.
NetworkStateType.OTHER = "OTHER"Active network connection over other network connection types.
错误代码
| Code | Description |
|---|---|
| ERR_NETWORK_IP_ADDRESS | 在 Android 上,在 getIpAddressAsync 中尝试访问 WifiManager 时,可能会出现未知的 Wi-Fi 主机。在 iOS 上,则无法检索任何网络接口。 |
| ERR_NETWORK_UNDEFINED_INTERFACE | 在 getMacAddressAsync 中传入了未定义的 interfaceName 作为参数。 |
| ERR_NETWORK_SOCKET_EXCEPTION | 在 getMacAddressAsync 中创建或访问 socket 时遇到错误。 |
| ERR_NETWORK_INVALID_PERMISSION_INTERNET | 在 getMacAddressAsync 中,android.permission.ACCESS_WIFI_STATE 的权限无效。 |
| ERR_NETWORK_NO_ACCESS_NETWORKINFO | 无法访问网络信息 |