react-native-netinfo: iOS NetInfoState isInternetReachable is null

Environment

System:
    OS: macOS 10.15.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    Memory: 9.34 GB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 13.10.1 - /var/folders/jz/j6fwfjfs5wq632p1_y8tgv3r0000gp/T/yarn--1587543476817-0.22339286857290186/node
    Yarn: 1.22.4 - /var/folders/jz/j6fwfjfs5wq632p1_y8tgv3r0000gp/T/yarn--1587543476817-0.22339286857290186/yarn
    npm: 6.13.7 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6200805
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.13.0 => 16.13.0 
    react-native: 0.61.5 => 0.61.5

Platforms

iOS

Versions

  • iOS: 13.0
  • react-native-netinfo: 5.7.1
  • react-native: 0.61.5
  • react: 16.13.0

Description

Under iOS you can simulate network stages. I wanted to see if NetInfoState isInternetReachable shows when 100% packet loss occurs. The variable is “null”, but when I turn off WLAN and mobile data, it shows false. Even if the internet connection is stable, it shows “null”.

Reproducible Demo

What I set is: Settings > Developer > Network Link Conditioner > Enable and 100% Loss and Connect to Wifi or Mobile Connection

async function showStatus() {
  const netState = await NetInfo.fetch();
  Alert.alert("State", JSON.stringify(netState, null, 2));
}

Best regards ZanderCodes

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 24
  • Comments: 19 (5 by maintainers)

Most upvoted comments

@mikehardy No time to dig into native module when it could be easily solved on JS side. Code is quite straightforward (for anyone interested):

const _checkInet = () => {
  return new Promise((resolve) => {
    let loaded, resolved = false;
    let xhr = new XMLHttpRequest();
    xhr.timeout = 500;
    xhr.open('GET', 'https://www.apple.com/', true);
    setTimeout(()=> {
      if (!loaded) {
        xhr.abort();
        resolved = true;
        return resolve(false)
      }
    }, 500)
    xhr.onload = ()=> {if (xhr.status === 200 && !resolved) {
      loaded = true;
      return resolve(true)
      }}
    xhr.ontimeout = ()=> {return resolve(false)}
    xhr.onerror = ()=> {if (!resolved) {return resolve(false)}}
    xhr.send()
  })
};

@dev-andremonteiro proven to work in my production app even on 3G. To be on the safe side just increase timeouts (mine are around 1500, don’t remember for now). Once again: iPhone without access to apple.com is almost unusable by design, so as dev you can be pretty sure that user has access. To google/amazon - could be a question, to apple - as was said, never.

On ios paltform, I also get this issue, vpn can sloved it for me.