Search code examples
reactjsreact-nativenetwork-programmingios-simulator

How to Simulate Internet Connection on React Native iOS Simulator?


I'm trying to simulate internet connection status on react native ios simulator.

I used NetInfo npm to persistently check for internet connection status.

const unsubscribe = NetInfo.addEventListener(async (state) => {                    
    console.log("Is isInternetReachable?", state.isInternetReachable);                                                            
});    

I tried using "Network Link Conditioner" to simulate internet connection status by switching between 100% loss and Wi-Fi Preset Profiles, but it is not changing the connection status.

enter image description here


Solution

  • you can try change the configuration of NetInfo to get a better control of how it will check for internet connection

    https://github.com/react-native-community/react-native-netinfo#configure

    NetInfo.configure({
      reachabilityUrl: 'https://clients3.google.com/generate_204',
      reachabilityTest: async (response) => response.status === 204,
      reachabilityLongTimeout: 60 * 1000, // 60s
      reachabilityShortTimeout: 5 * 1000, // 5s
      reachabilityRequestTimeout: 15 * 1000, // 15s
    });
    

    Important: Note that calling this will stop all previously added listeners from being called again. It is best to call this right when your application is started to avoid issues.