Online / Offline events in Phonegap / Apache Cordova

online-offline-events-in-phonegap
online-offline-events-in-phonegap
online-offline-events-in-phonegap

Online / Offline events in Phonegap / Apache Cordova is uses Network Information Plugin. We can check the user’s network information using network plugin. In this tutorial we’re going to find, whether user is connected to internet or not.

Step 1:

Create Phonegap Project & Add Platform

Step 2:

Add Network Information Plugin ( https://github.com/apache/cordova-plugin-network-information ) for accessing user’s network information

For 5.0+

cordova plugin add cordova-plugin-network-information

For Older Version

cordova plugin add org.apache.cordova.network-information

Create Event Listener for online / offline events.

Add these line in your deviceready events

document.addEventListener("online",chkOnline,false);
document.addEventListener("offline",chkOffline,false);

Add these function for alert purpose

function chkOnline()
{
//add your own functionalities, if user is online
 alert("you're in Online");
}
function chkOffline()
{
//add your own functionalities, if user is offline
 alert("you are in offline");
}

where chkOnline,chkOffline are the functions, it’ll return when success.