平台
此问题与 iOS 有关。
版本
- iOS:12.4.1
- 反应原生网络信息:“^5.5.0”
- 反应原生:“0.61.5”
- 反应:“16.9.0”
描述
我正在使用动态数据在项目中实施网络状态检查,并在进行 API 调用或应用程序状态更改时检查互联网可访问性。
我的问题是,isInternetReachable
即使可以访问互联网,只要应用程序从后台进入前台,密钥总是会在返回正确状态之前返回一分钟。该问题发生在 iOS 真实设备上,并且在 iOS 模拟器和 android 真实设备上一切正常。
可重现的演示
let state;
componentDidMount() {
// Adding AppState event listener
AppState.addEventListener('change', this._handleAppStateChange);
// Adding NetInfo event listener
this._subscription = NetInfo.addEventListener(
this._handleConnectionInfoChange,
);
}
componentWillUnmount() {
// Removing AppState event listener
AppState.removeEventListener('change', this._handleAppStateChange);
// Removing NetInfo event listener
this._subscription && this._subscription();
}
_handleAppStateChange = async nextAppState => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
state = await NetInfo.fetch();
});
}
if (this.state.appState.match(/active|inactive/) && nextAppState === 'background') {
state = await NetInfo.fetch();
});
}
if (this.state.appState.match(/active|background/) && nextAppState === 'inactive') {
state = await NetInfo.fetch();
});
}
this.setState({ appState: nextAppState });
};
getJobs = async (index) => {
// fetching status of internet connection
state = await NetInfo.fetch();
if (state.isInternetReachable === false) {
Alert.alert(Constants.k_APP_NAME, Constants.k_OFFLINE_TEXT);
} else {
this.props.callService(item, false);
}
}
}