How to check internet connection in React Native application for both iOS and Android?

后端 未结 7 2212
孤城傲影
孤城傲影 2021-02-07 12:52

I have a React Native application and I\'m seeking to add functionality that checks if there is an active internet connection when the app first starts up, and continuously ther

相关标签:
7条回答
  • 2021-02-07 13:13

    For it react-native provide a library called netinfo: please check on: https://github.com/react-native-community/react-native-netinfo

    It provides an api to check the connectivity and its type.

    NB: if you are using RN < 0.60: https://facebook.github.io/react-native/docs/netinfo.html

    0 讨论(0)
  • 2021-02-07 13:13

    Create NetworkUtills.js

    import NetInfo from "@react-native-community/netinfo";
    export default class NetworkUtils {
      static async isNetworkAvailable() {
        const response = await NetInfo.fetch();
        return response.isConnected;
    }}
    

    Use anywhere like this

    const isConnected = await NetworkUtils.isNetworkAvailable()
    
    0 讨论(0)
  • 2021-02-07 13:17

    Please read this https://reactnativeforyou.com/how-to-check-internet-connectivity-in-react-native-android-and-ios/ link.

    import React, { Component } from "react";
    import { View, Text, Button, Alert, NetInfo, Platform } from "react-native";
    
    export default class componentName extends Component {
      constructor(props) {
        super(props);
        this.state = {};
      }
    
      CheckConnectivity = () => {
        // For Android devices
        if (Platform.OS === "android") {
          NetInfo.isConnected.fetch().then(isConnected => {
            if (isConnected) {
              Alert.alert("You are online!");
            } else {
              Alert.alert("You are offline!");
            }
          });
        } else {
          // For iOS devices
          NetInfo.isConnected.addEventListener(
            "connectionChange",
            this.handleFirstConnectivityChange
          );
        }
      };
    
      handleFirstConnectivityChange = isConnected => {
        NetInfo.isConnected.removeEventListener(
          "connectionChange",
          this.handleFirstConnectivityChange
        );
    
        if (isConnected === false) {
          Alert.alert("You are offline!");
        } else {
          Alert.alert("You are online!");
        }
      };
    
      render() {
        return (
          <View>
            <Button
              onPress={() => this.CheckConnectivity()}
              title="Check Internet Connectivity"
              color="#841584"
              accessibilityLabel="Learn more about this purple button"
            />
          </View>
        );
      }
    }
    
    0 讨论(0)
  • 2021-02-07 13:29

    NetInfo has been removed from React-Native. It can now be installed and imported from 'react-native-netinfo' instead from 'react-native'. See https://github.com/react-native-community/react-native-netinfo

    0 讨论(0)
  • 2021-02-07 13:31

    The Android manifest file is here: \android\app\src\main\AndroidManifest.xml. Further use this library to do your require https://github.com/react-native-community/react-native-netinfo

    0 讨论(0)
  • 2021-02-07 13:32

    I ran into this today and found solution which I believe is the best. Its gonna continuously search for network changes and display them accordingly.

    I tested it with expo install @react-native-community/netinfo and its working flawlessly.

    import {useNetInfo} from "@react-native-community/netinfo";
    
    const YourComponent = () => {
      const netInfo = useNetInfo();
    
      return (
        <View>
          <Text>Type: {netInfo.type}</Text>
          <Text>Is Connected? {netInfo.isConnected.toString()}</Text>
        </View>
      );
    };
    
    0 讨论(0)
提交回复
热议问题