Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 thereafter.

If there is no internet connection, I'm seeking to display a message saying "Internet connection not detected" with a button to "Try again"; if there is an internet connection, I'm seeking to load a page (WebView).

I'm also seeking to support both iOS and Android devices; I've researched this independently and have found a couple libraries on GitHub. However, many require an extra step of including a permissions addition in Android Manifest XML, however I don't see an Android Manifest XML file in my app; why does only Android need a manifest?

Any help is appreciated; thanks and take care.

like image 470
zstardust225 Avatar asked Jul 31 '19 18:07

zstardust225


People also ask

How do I check my connection continuously in React Native?

NetInfo notifies continuously about the network state whether it is online or offline. React Native's NetInfo component is used to check internet connectivity status runtime in react native application. NetInfo is properly works in both android and iOS mobile platforms.

Is React Native different for iOS and for Android?

With React Native, developers can write real, natively rendering mobile applications for iOS and Android. It helps build apps on two platforms at once, while maintaining the look, feel, and productivity of an app built on the specific iOS or Android platform.

Can React Native app run without internet?

You'll need internet connection to install the React Native CLI and create your app (this will fetch React Native and its JavaScript dependencies). You'll also need internet connection the first time you run react-native run-android because this will download third-party Android dependencies.


2 Answers

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>     );   } } 
like image 191
Mirzohid Akbarov Avatar answered Sep 23 '22 09:09

Mirzohid Akbarov


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"; import {View, Text} from "react-native"; const YourComponent = () => {   const netInfo = useNetInfo();    return (     <View>       <Text>Type: {netInfo.type}</Text>       <Text>Is Connected? {netInfo.isConnected.toString()}</Text>     </View>   ); }; 
like image 22
Lukáš Brýla Avatar answered Sep 19 '22 09:09

Lukáš Brýla