skip to Main Content

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.

3

Answers


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

    if (!window.navigator.onLine) {
       alert("you are offline");
       window.close();
    }
    
    Login or Signup to reply.
  2. @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

    Login or Signup to reply.
  3. You can make use of "react-native-netinfo" npm package which gives us boolean value (true / false). You can implement your logic based on this boolean value whether it is a fallback UI or you can show alert to user as "internet is not connected" something like this.
    You can learn more use this link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search