Answer the question
In order to leave comments, you need to log in
How to get network information in Cordova application?
Good day to all.
There is an application written on ionic. If there is Internet, you need to take data from an external server. If there is no Internet, take the data or a local copy.
Themselves both principles of obtaining certain data are implemented. It is not possible to determine the state of the network at the time of startup.
Such a principle is routes, when switching to which, in the resolve block, the service is twitching - which returns data.
And so it is necessary for me somewhere at the moment before resolve is fulfilled. know the state of the network.
But something doesn't come out. Who can tell how to implement it correctly?
Answer the question
In order to leave comments, you need to log in
you should add to the ngCordova application and use the ngcordova.com/docs/plugins/network plugin and use
it to check the network status
Yes, I'm also interested in seeing best practice :D in this regard.
And then everywhere there are guides and so on, but there is nothing about this nuance that the Internet may not exist or may disappear.
They can’t add an implementation to frameworks or templates, at least code.
So far I have found (the code under AngularJS did not check, if anyone confirms that it is working, it would be ok):
controller('HomeCtrl', function ($scope, $stateParams, $rootScope) {
$scope.error = 'No internet connection';
document.addEventListener('deviceready', init, false);
function init() {
document.addEventListener('online', toggleCon, false);
document.addEventListener('offline', toggleCon, false);
}
function toggleCon(e) {
if (e.type == 'offline') {
//if offline
} else {
//if online
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question