saiRam89
saiRam89

Reputation: 375

NetInfo in react native not working in ios

I have used NetInfo in react native in Android it's work fine but in ios when i run in simulator it's shows always internet is offline.Later in found Asynchronously check internet Connection in both ios and android platform but i can't get correct example please help me.

Upvotes: 0

Views: 5716

Answers (2)

Akhilesh Sinha
Akhilesh Sinha

Reputation: 881

IF THIS DOESN'T WORK ......

import {NetInfo} from 'react-native';

     NetInfo.isConnected.fetch().then(isConnected => {

      if(isConnected)
           {
                console.log('Your are connected....')
            }

            else
            {
                console.log('Your are disconnected....')
            }

        })

this may help..... How to check net Info in React Native iOS?

Upvotes: 0

Saravana Kumar
Saravana Kumar

Reputation: 3396

For ios You have to add following things :

1. Import netinfo :

import {NetInfo} from 'react-native';

2. Add Listener :

componentDidMount() {
  NetInfo.isConnected.addEventListener('change', this.handleConnectionChange);
}

componentWillUnmount() {
    NetInfo.isConnected.removeEventListener('change', this.handleConnectionChange);
}

handleConnectionChange = (isConnected) => {
    console.log(isConnected);
}

Upvotes: 0

Related Questions