-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS: NetInfo.isConnected returns always false #8615
Comments
I can also reproduce this with RN 0.29.0. I was running on 0.26.2 earlier and it worked fine with it. Going to try building from source and debug. |
+1 |
I've reported the same issue before: #8469 Quick workaround is to add an event handler |
+1 and same issue with NetInfo.fetch().done() - always returns |
I solved it this way: componentDidMount() {
const dispatchConnected = isConnected => this.props.dispatch(setIsConnected(isConnected));
NetInfo.isConnected.fetch().then().done(() => {
NetInfo.isConnected.addEventListener('change', dispatchConnected);
});
} |
+1 |
we have found this bug in 0.29.2.. |
we have found this bug in 0.28 |
0.30.0 still not working |
The relevant code hasn't changed so it makes sense that it's still not working. Note that I previously reported the bug here: #8469 where it details the cause and has sample code attached. Because of this, I think it makes sense to leave this closed. The number of open RN issues grows daily so it's important to do our part and consolidate where possible. |
I noted the same Issue today |
I am using 0.30.0 version , And i'm also found this error. |
the same issue to me, I am using RN@0.30.0 and the fetch always return unknown. But we can use addEventListener instead.
instead of:
|
I am having this issue on 0.31, will this be fixed? |
same to 0.32 |
@knowbody How your snippet work? I need to define a setIsConnected method in my class or as a global function or anything else? Because, actually I have an error who said |
@florentsorel so in my project I use redux for state management and
|
+1 |
I'm having this issue on v0.34.1 Edit: Updated to v0.35.0 and same problem. |
same on v0.35.0 |
I am having the same issue on v0.36.0 |
@knowbody snippet works for me in 0.36 |
The underlying code hasn't really changed so I would be surprised if this is "fixed". I put fix in quotes because it seems like the motivation for this change is to only set up the native machinery that watches the network status if the application code is interested in observing it. Apps register interest by adding an event listener which then starts checking reachability. At this point it seems like this is the desired behaviour so perhaps a documentation change is required. My apps have code that is very similar to what @knowbody posted. I don't call i assume there is connectivity, add the listener, and then the state will be updated when the connectivity state is no longer unknown. |
0.34.1 still not working |
Same issue with RN 0.37.0 |
Upgraded to 55 specifically because the changelog said it's supposed to fix this -- still broken (0.55.3). I am able to get it to work using a similar solution to @yuyao110120: NetInfo.isConnected.fetch().then(() => {
NetInfo.isConnected.fetch().then(isConnected =>
// isConnected is now the correct value
});
}); |
I no longer trust async function handleConnectivityChange(status) {
const { type } = status;
let probablyHasInternet;
try {
const googleCall = await fetch(
'https://google.com', {
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Pragma: 'no-cache',
Expires: 0,
},
});
probablyHasInternet = googleCall.status === 200;
} catch (e) {
probablyHasInternet = false;
}
console.log(`@@ isConnected: ${probablyHasInternet}`);
}
NetInfo.getConnectionInfo().then(handleConnectivityChange);
NetInfo.addEventListener('connectionChange', handleConnectivityChange); |
@SudoPlz I pretty did the same thing on my apps, works just fine. But the |
@SudoPlz I noticed that
|
Ok thanks for letting me know @abdallamohamed I updated the comment. |
same as @kylevdr, worked reliably but adding a small timeout before 2nd call seems to help with flicker where it will disconnected for a split 2nd right after unlocking Edit: fwiw this works in 55.3, but not 55.4 |
Adding an initial connection event listener will cause
Thanks #8469 Make sure to import this file or else it will not execute. P.S. If you are using |
None of the solutions above worked when running the app in debug mode on an iPhone 8 (React Native 0.55.3, iOS 11.3.1) in the following scenario: Close the app and start it with mobile data and wifi off. Any subsequent events are lost with all solutions mentioned above (except for @SudoPlz's answer). Only running the application in release mode on the device or in debug mode on a simulator triggered any |
@nlindroos so do you actually confirm that |
In the specific case of starting the application in offline mode on a physical iOS device, running in release mode was the only working solution to get any |
I can say that combination of the solutions that @woodpav and @abdallamohamed worked for me |
I must ping Google server frequently to check for internet connection 😕 |
I am using iPhone 8 - 11.3 simulator and on RN version 0.55.4 NetInfo.isConnected.fetch().then(isConnected => console.log('isConnected', isConnected)) When switching back to RN version 0.55.3 it seems to be working again. |
@octopitus , @martinentelect
In my
|
@assafb81 Thank you! This helped my use case. |
In my case, the event listener works perfectly on iOS but not at all on Android (on the emulator at least).
|
Use
|
@tannera, |
On |
Still not fixed on 0.56 |
@SudoPlz I've hidden your comment on the 0.57 discussion thread as off-topic. As you know, there's hundreds of open issues in the repo, but that thread is focused on getting a 0.57 release out. This thread on a closed is pretty much invisible to maintainers, and I'm surprised that six months after a potential fix landed, no one has opened a new issue. If this is something you'd like to see fixed in master, please open a new issue with details, and ideally, send a PR. |
Here's a minimal reproducible example (tested on iOS device), with some code from @woodpav shared above to work around the problem commented out at the top: https://snack.expo.io/@notbrent/netinfo-repro. |
Summary: This is fixing facebook#8615. The problem was that, on initialization, the `_connectionType` variable was still set to its default value of `RCTConnectionTypeUnknown`. The exposed API method did nothing to determine this unless a subscription had be established and then the method would simply return the last reported value. Instead, the exposed oneshot API call now actually checks the connection status through the same methods as the subscription and updates RCTNetInfo’s values before returning. In order to avoid reporting events without a subscription, a flag is set and unset on calls to start/stopObserving. - start app - observe the (in)correct reporting of the manual status - change network status to offline - press refresh - observe the manual fetch - start subscription - change network status to online - press refresh to show that the manual refresh works (only now working for current RN version) - change network status to offline - stop subscription - change network status to online - press refresh to show manual refresh does(n't) work without subscription - start subscription to show it updates to current Current Behavior: https://drive.google.com/file/d/1Ods6HORgp_vfm1mQVjGwhtH1D7issxjo/view?usp=sharing Fixed Behavior: https://drive.google.com/file/d/11H1UOF33LeMGvXEOoapU62ARDSb7qoYv/view?usp=sharing [IOS] [BUGFIX] [Libraries\Network\RCTNetInfo.m] - Fixed facebook#8615, `iOS: NetInfo.isConnected returns always false`, by decoupling the fetch from the status of the subscription. Closes facebook#17397 Differential Revision: D7102771 Pulled By: hramos fbshipit-source-id: ea11eb0b1a7ca641fc43da2fe172cf7b2597de4a
I am currently running RN 0.28.0...
I would do a PR but I can't code for iOS.
The text was updated successfully, but these errors were encountered: