ionic: How to check the internet connectivity? is mobile data on/off? in ionic mobile app

Hello all, today let’s see how to check the internet connectivity is connected or disconnected to our ionic mobile app.

First, install the Network plugin to your ionic mobile app.

ionic cordova plugin add cordova-plugin-network-information

npm install @ionic-native/network

And then, import the Network plugin into your app.module.ts file and add it to the providers.

Next, add the Network plugin to your app.component.ts file, then include it in constructor and add onDisconnect() method and onConnect() under constructor or initializeApp() method.

please note that the following also can be added in this.platform.ready() under initializeApp() method. Here, I have added my code in platform.ready() under initializeApp() method.

Then, you can handle your toast messages or alert messages according to the network connectivity.

Here, I want my app to close (exit) when the app disconnect from the internet. So I have manage it as follows.

That’s all

Now you can run/build your app on the preferred device.

Please kindly note that this will work perfectly on the device.

Thanks for reading this article! if you have any questions, feel free to comment down.

Cheers!!!