NetInfo总是返回false

时间:2019-10-23 14:33:50

标签: android ios react-native react-native-community-netinfo

在我的本机应用程序中,我需要检查连接以显示/隐藏一些信息,但每次检查连接时,state.isConnected总是返回false,尽管连接为true。

componentDidMount() {
     navigator.geolocation.getCurrentPosition(
          (position) => {
            this.connectionEventHandler(position);
          },
          error => {
            if (error.code) {
              alert("error 1: " + JSON.stringify(error))
              this.props.navigation.goBack();
            }
          },
          Platform.OS==='android' ? {} : {enableHighAccuracy: false, timeout: 20000 }
        );
  }
this is my function to handle connection event:

connectionEventHandler = (position) => {

    this.unSubscribe = NetInfo.addEventListener(state => {
      if (state.isConnected) {
        this.fetchProfileStatus(position);
        this.unSubscribe();
      } else {
        this.fetchProfileStatus(position);
      }
    });
  }

componentWillUnmount() {
    this.unSubscribe();
  }

0 个答案:

没有答案