gpt4 book ai didi

ios - iOS:NetInfo.isConnected始终返回false

转载 作者:行者123 更新时间:2023-12-01 18:37:53 25 4
gpt4 key购买 nike

netInfo React Native总是在ios模拟器中返回false

NetInfo.isConnected.fetch().then(isConnected => {
if (isConnected == true) {
this.setState({isOnline: true})
this.checkForToken()
}
else {
this.setState({isOnline: false})
this.checkTokenOffline()
}
});

NetInfo.isConnected.fetch().then().done(() => {
NetInfo.isConnected.addEventListener('connectionChange', (isConnected) => this.dispatchConnected());
})

最佳答案

这是known bug,它总是给出false。您需要附加一个侦听器,然后使用它。

yourFunction = () => {
NetInfo.isConnected.fetch().then(isConnected => {
//Always false, so ignore/do nothing here
});
NetInfo.isConnected.addEventListener(
'connectionChange',
this.handleFirstConnectivityChange
);
}

handleFirstConnectivityChange = (isConnected) => {
if (isConnected == true) {
this.setState({isOnline: true})
this.checkForToken()
}
else {
this.setState({isOnline: false})
this.checkTokenOffline()
}
}

关于ios - iOS:NetInfo.isConnected始终返回false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com