Check Internet Connection in Ionic

If you google “checking internet connection in Ionic” you might probably be asked to use the Ionic Network module.

Instead of using any modules/libraries, it is possible to check the internet connection with JavaScript and Angular itself. I will show you how in this post.

Check for Internet Connection on App launch

In the app.component.ts file if you add the following code to the constructor.

This makes use of the Angular’s navigator global object to check for internet connectivity. The navigator.onLine returns a Boolean, true if there is internet connection and false when there is no connection. An example of this being used is shown below.

Check for internet connection when app is being used.

When the app is running and being used, if there is no internet connection, we could use the JavaScript’s own Window object.

Adding the following code in the app.component.ts’s constructor. will check if the application went offline when application is being used.

And of course you can do the same with online listener to check when internet connection returns

Senior Software Engineer at 99x | Freelance developer | Interested in History and Philosophy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store