首页指南参考教程

Expo 网络 iconExpo 网络

提供对设备网络的访问(例如其 IP 地址、MAC 地址和飞行模式状态)的库。

Android
iOS
Web

expo-network 提供有关设备网络的有用信息,例如 IP 地址、MAC 地址和飞行模式状态。

¥expo-network provides useful information about the device's network such as its IP address, MAC address, and airplane mode status.

安装

¥Installation

Terminal
npx expo install expo-network

If you are installing this in an existing React Native app (bare workflow), start by installing expo in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.

配置

¥Configuration

在 Android 上,该模块需要访问网络和 Wi-Fi 状态的权限。权限 ACCESS_NETWORK_STATEACCESS_WIFI_STATE 是自动添加的。

¥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.

API

import * as Network from 'expo-network';

Methods

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.

Returns:

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.

Returns:

Promise<NetworkState>

A Promise that fulfils with a NetworkState object.

Example

await Network.getNetworkStateAsync();
// {
//   type: NetworkStateType.CELLULAR,
//   isConnected: true,
//   isInternetReachable: true,
// }
Only for:
Android

Network.isAirplaneModeEnabledAsync()

Tells if the device is in airplane mode.

Returns:

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

Types

NetworkState

NameTypeDescription
isConnected
(optional)
boolean

If there is an active network connection. Note that this does not mean that internet is reachable. This field is false if the type is either Network.NetworkStateType.NONE or Network.NetworkStateType.UNKNOWN, true otherwise.

isInternetReachable
(optional)
boolean

If the internet is reachable with the currently active network connection. On Android, this depends on NetInfo.isConnected() (API level < 29) or ConnectivityManager.getActiveNetwork() (API level >= 29). On iOS, this value will always be the same as isConnected.

type
(optional)
NetworkStateType

A NetworkStateType enum value that represents the current network connection type.

Enums

NetworkStateType

An enum of the different types of devices supported by Expo.

NetworkStateType Values

BLUETOOTH

NetworkStateType.BLUETOOTH = "BLUETOOTH"

Active network connection over Bluetooth.

CELLULAR

NetworkStateType.CELLULAR = "CELLULAR"

Active network connection over mobile data or DUN-specific mobile connection when setting an upstream connection for tethering.

ETHERNET

NetworkStateType.ETHERNET = "ETHERNET"

Active network connection over Ethernet.

NONE

NetworkStateType.NONE = "NONE"

No active network connection detected.

OTHER

NetworkStateType.OTHER = "OTHER"

Active network connection over other network connection types.

UNKNOWN

NetworkStateType.UNKNOWN = "UNKNOWN"

The connection type could not be determined.

VPN

NetworkStateType.VPN = "VPN"

Active network connection over VPN.

WIFI

NetworkStateType.WIFI = "WIFI"

Active network connection over WiFi.

WIMAX

NetworkStateType.WIMAX = "WIMAX"

Active network connection over Wimax.

错误代码

¥Error codes

代码描述
ERR_NETWORK_IP_ADDRESS在 Android 上,尝试在 getIpAddressAsync 中访问 WifiManager 时可能存在未知的 Wi-Fi 主机。在 iOS 上,无法检索网络接口。
ERR_NETWORK_UNDEFINED_INTERFACE未定义的 interfaceName 作为参数传递到 getMacAddressAsync 中。
ERR_NETWORK_SOCKET_EXCEPTION创建或访问 getMacAddressAsync 中的套接字时遇到错误。
ERR_NETWORK_INVALID_PERMISSION_INTERNETgetMacAddressAsync 中存在 android.permission.ACCESS_WIFI_STATE 的无效权限。
ERR_NETWORK_NO_ACCESS_NETWORKINFO无法访问网络信息
Expo 中文网 - 粤ICP备13048890号