I am working on Ionic Framework and cordova. I want to submit some data to server
but when these two condition are satisfied
1. device is online.
2. data (to submit) is available .
i am checking this cordova network plugin documentation but i am confuse that there is too many quirks in it. How can i write valid code for all devices(means android, win Phone, IPhone etc..) and also how can i get user IP Address.??
i have tried this, working fine in browser but afraid to implement cause mobile phone can ignore this line (i guess)
$scope.checkOnline = navigator.onLine;
It's easy, you only need to use two Cordova plugins for this:
- Plugin 1: cordova-plugin-networkinterface
Example:
networkinterface.getIPAddress(function (ip) {
alert(ip);
});
Working example and tutorial, click here.
- Plugin 2: cordova-plugin-network-information
Example:
document.addEventListener("online", onOnline, false);
function onOnline() {
// Handle the online event
}
Plugin documentation, click here.
It is actually hard because of quirks and because of the uncertainty with connectivity to actual Internet. For example
navigator.online
isn't reliable for checking the actual connectivity to your online services, but if it says you are offline, you most certainly are offline.
Instead, the best way to check your connection is to try actually get some response from service that you know will be up 99.99% of time.
Here is also a good blog post series about this issue. You might wan't to consider something like heartbeat for your application as explained there. Also it is generally a good idea to bind into the online & offline events of Cordova so that you can for example check connectivity instantly if online event happens.