How to Detect Device is Connected to Internet Using Cordova

Recently, I have been working on a Hybrid Application. I encountered a requirement to detect whether the device is connected to the internet or not. Since I was developing a Hybrid Application I had an option to detect Internet using HTML5 . However this approach had a problem in browsers that do not support HTML5. A better approach to detect internet connectivity of a device is through Cordova (PhoneGap).

You can detect whether a device is online or offline in an "if" condition as follows:

detect-Device-using-Cordova.jpg

If the device is connected to the internet then you can check the type of connection in the device using Cordova as well. You can do that as in the following:

connection-in-device-using-Cordova.jpg

Make sure you have added the reference of Cordova to work with this.

added-the-reference-of-cordova.jpg

The best practice is to create a function to detect the device connectivity status and call that function when the device is ready. I hope you find this article useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all