Skip to content
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

NetInfo returns true when connected to [WiFi|cellular] but Internet is not reachable #18368

Closed
sfm2222 opened this issue Mar 14, 2018 · 16 comments
Labels
Bug 🌐Networking Related to a networking API. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@sfm2222
Copy link

sfm2222 commented Mar 14, 2018

When I have mobile data on(cellular internet - device is connected via Edge, 3G, WiMax, or LTE) and my data package is finished (i.e: I cannot use internet) Netinfo always return true.

Environment

Environment:
OS: macOS High Sierra 10.13.3
Node: 8.9.1
Yarn: Not Found
npm: 5.6.0
Watchman: Not Found
Xcode: Xcode 9.2 Build version 9C40b
Android Studio: Not Found

Packages: (wanted => installed)
react: 16.0.0 => 16.0.0
react-native: 0.54 => 0.54

Expected Behavior

Ideally netinfo should return false when there is no internet connectivity. Just turning on the mobile data should not enough for it to depict that device has internet connection.

Actual Behavior

  NetInfo.isConnected.fetch().then(isConnected => {
     alert(isConnected);
   })

Steps to Reproduce

Just turn off the wifi and turn on the data connection on the mobile device which has no sim or with the sim which has no credit. It is happening on both iOS & Android.

@react-native-bot
Copy link
Collaborator

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest stable release?

Thank you for your contributions.

How to ContributeWhat to Expect from Maintainers

@react-native-bot react-native-bot added Old Version ⏪ Ran Commands One of our bots successfully processed a command. labels Mar 14, 2018
@sfm2222 sfm2222 changed the title Cellular internet connected but no internet Package, NEtInfo returns true Cellular internet connected but no internet Package, NetInfo returns true Mar 14, 2018
@hramos hramos changed the title Cellular internet connected but no internet Package, NetInfo returns true NetInfo returns true when connected to cellular but data plan has been exhausted Mar 14, 2018
@hramos hramos added the 🌐Networking Related to a networking API. label Mar 14, 2018
@sfm2222
Copy link
Author

sfm2222 commented Mar 15, 2018

It is just the same on new version as well. Can you please check it. Thank you

@hramos
Copy link
Contributor

hramos commented Mar 15, 2018

Can you edit your post and ensure the repro steps include the specific version you tested this on?

@sfm2222
Copy link
Author

sfm2222 commented Mar 16, 2018

Updated.

@hramos
Copy link
Contributor

hramos commented Mar 16, 2018

Thank you!

@hramos hramos changed the title NetInfo returns true when connected to cellular but data plan has been exhausted NetInfo returns true when connected to [WiFi|cellular] but Internet is not reachable Mar 20, 2018
@hramos
Copy link
Contributor

hramos commented Mar 20, 2018

We'll need to investigate if this is working as intended. NetInfo is correctly showing that the device is connected to a cellular or WiFi network. The question is - should this also reflect Internet reachability? Personally, I don't think it should, but we should still provide a way for the developer to test reachability towards an arbitrary host.

@react-native-bot react-native-bot added the Platform: macOS Building on macOS. label Mar 20, 2018
@sfm2222
Copy link
Author

sfm2222 commented Mar 21, 2018

@hramos thank you.

I think most of the developers are not much concerned with the fact that device is either connected to wifi/cellular or not(leaving aside if you are keen about data package spent in certain scenerio), the major concern is to check the internet reachability, to limit the actions which should not performed by the app if there is no internet connection.

For the temporary solution I have just sent a ping request to my WebAPI through code before making a request if it's not reachable so I assume there is no internet connection. Either this is incorrect WebAPI server could be down.

return fetch('URL')
      .then((response) => {
      })
  .catch((error) => {
        if(error == 'TypeError: Network request failed'){
          Alert.alert('Something went wrong', 'Kindly check if the device is connected to stable cellular data plan or WiFi.'); 
        }
       });

But I am still in favour of the fact that internet reachability check should be provided as a part of the library.

@hramos hramos removed the Platform: macOS Building on macOS. label Mar 29, 2018
@aylesm
Copy link

aylesm commented Apr 20, 2018

I have airplane mode on. So WiFi and Cell are turned off. The API is still reporting that I'm online.

The following function is always returning true for me....

  return new Promise(resolve => {
        NetInfo.isConnected.fetch().then(isConnected => {
          console.log('Device, is ' + (isConnected ? 'online' : 'offline'));
          resolve(isConnected)
        })
    })
}```

As a workaround I had previously implemented the following.  With an update to react-native 0.54.4 the API reports that I'm connected WiFi after disabling Wifi on iPhone X.

``//workaround for a bug on iOS where the isConnected function doesn't
//actually work.
export function isNetworkConnected() {
  return NetInfo.getConnectionInfo().then(connectionInfo => {
    console.log('connection info:', connectionInfo)
    if (connectionInfo.type.match(/unknown/i)) {
      return new Promise(resolve => {
        const handleFirstConnectivityChangeIOS = isConnected => {
          console.log('connection info:', isConnected)
          NetInfo.isConnected.removeEventListener('connectionChange', handleFirstConnectivityChangeIOS);
          resolve(isConnected);
        };
        NetInfo.isConnected.addEventListener('connectionChange', handleFirstConnectivityChangeIOS);
      });
    }
    reachability = connectionInfo.type.toLowerCase();
    return (reachability !== 'none' && reachability !== 'unknown');
  });
}``

@yangnana11
Copy link

Has anyone fixed this problem? I got the same situation with react-native 0.55.4
I can use connectionInfo.type === 'none' for android, but ios always return 'unknown' even with or without wifi...

@Meligy
Copy link

Meligy commented Sep 10, 2018

Does the following work as a workaround?

const connectionInfo = await NetInfo.getConnectionInfo();
const connectionIsLost = connection.effectiveType === 'unknown';

facebook-github-bot pushed a commit that referenced this issue Sep 12, 2018
Summary:
Fixes #20804, #8615, #18368 (comment)
Pull Request resolved: #20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
gengjiawen pushed a commit to gengjiawen/react-native that referenced this issue Sep 14, 2018
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
kelset pushed a commit that referenced this issue Sep 21, 2018
Summary:
Fixes #20804, #8615, #18368 (comment)
Pull Request resolved: #20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
@Vitormdias
Copy link

Having the same issue here, has anyone fixed this ?

@Vitormdias
Copy link

Does the following work as a workaround?

const connectionInfo = await NetInfo.getConnectionInfo();
const connectionIsLost = connection.effectiveType === 'unknown';

No

@stlst
Copy link

stlst commented Nov 28, 2018

I have the same issue as well. I think it's necessary to know Internet reachability, especially for WIFI that needs authentication.

@topwebtek7
Copy link

Does anyone know solution to this issue? i'm having similar issue

@Vitormdias
Copy link

Does anyone know solution to this issue? i'm having similar issue

https://github.com/tegraoss/react-native-connection-status

I made this work around, but it only works in android, in iOS it only makes a pooling so a I don't recommend to use it in production.

@hramos hramos removed the Bug Report label Feb 6, 2019
matt-oakes pushed a commit to matt-oakes/react-native that referenced this issue Feb 7, 2019
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
@cpojer
Copy link
Contributor

cpojer commented Feb 12, 2019

Heads up: we moved react-native-netinfo into its own repository, which should allow for making changes and fixes much faster. Please continue the discussion about this issue there: react-native-netinfo/react-native-netinfo#12

@cpojer cpojer closed this as completed Feb 12, 2019
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
@facebook facebook locked as resolved and limited conversation to collaborators Feb 12, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Feb 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug 🌐Networking Related to a networking API. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

10 participants