Home > Mobile >  How can I stop my user to open my React Native app on their phone until they will not turn on their
How can I stop my user to open my React Native app on their phone until they will not turn on their

Time:08-26

How can I stop my user to open my React Native app on their phone until they will not turn on their wifi or mobile data? and want to show a message or notification to turn on their internet connection to open the app.

When user will try to open the app a message/Notification should show like- You are offline, please turn on your internet connection to visit the app.

If user is already connected to the internet then don't need any message/Notification.

CodePudding user response:

try window.navigator.onLine to detect if the browser is online

if (!window.navigator.onLine) {
   alert("you are offline");
   window.close();
}

CodePudding user response:

@rahul you should use https://github.com/react-native-netinfo/react-native-netinfo

this package, which basically exposes methods via which you can check whether wifi or data is connected or not.

// Subscribe
const unsubscribe = NetInfo.addEventListener(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
});

// Unsubscribe
unsubscribe();

here in state.isCOnnected you will get to know whether device data is turned off or not.

Hope it helps. feel free for doubts

  • Related