This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
@react-native-community/netinfo
一个提供网络信息访问能力的跨平台 API
@react-native-community/netinfo允许你获取连接类型和连接质量的信息。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project. Then, follow the installation instructions provided in the library's README or documentation.
API
要导入此库,请使用:
import NetInfo from '@react-native-community/netinfo';
如果你只想获取一次网络连接的信息,可以使用:
NetInfo.fetch().then(state => { console.log('Connection type', state.type); console.log('Is connected?', state.isConnected); });
或者,如果你更希望订阅网络状态更新(这样你就可以在网络状态发生变化时随时运行代码/执行操作),请使用:
const unsubscribe = NetInfo.addEventListener(state => { console.log('Connection type', state.type); console.log('Is connected?', state.isConnected); }); // To unsubscribe to these update, just use: unsubscribe();
访问 SSID
要访问 ssid 属性(位于 state.details.ssid 下),还需要完成一些额外的配置步骤:
- 使用
Location.requestForegroundPermissionsAsync()或Location.requestBackgroundPermissionsAsync()请求位置权限。
仅限 iOS
-
在 app.json 的
ios.entitlements下添加com.apple.developer.networking.wifi-info权限:app.json -
在你的应用 App Identifier 中勾选 Access Wi-Fi Information, 可在此处找到该选项。
-
使用
eas build --platform ios或npx expo run:ios重新构建你的应用。
了解更多
获取有关 API 及其用法的完整信息。