Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aaronksaunders/5bf84c5da80e783bb36f to your computer and use it in GitHub Desktop.
Save aaronksaunders/5bf84c5da80e783bb36f to your computer and use it in GitHub Desktop.
var myApp = angular.module('myApp').service('CordovaNetwork', ['$rootScope', '$ionicPlatform', '$q', function($rootScope, $ionicPlatform, $q) {
// Get Cordova's global Connection object or emulate a smilar one
var Connection = window.Connection || {
'ETHERNET' : 'ethernet',
'WIFI' : 'wifi',
'CELL_2G' : 'cell_2g',
'CELL_3G' : 'cell_3g',
'CELL_4G' : 'cell_4g',
'CELL' : 'cell',
'EDGE' : 'edge',
'UNKNOWN' : 'unknown'
};
var asyncGetConnection = function () {
var q = $q.defer();
$ionicPlatform.ready(function () {
if(navigator.connection) {
q.resolve(navigator.connection);
} else {
q.reject('navigator.connection is not defined');
}
});
return q.promise;
};
return {
isOnline: function () {
return asyncGetConnection().then(function(networkConnection) {
var isConnected = false;
switch (networkConnection.type) {
case Connection.ETHERNET:
case Connection.WIFI:
case Connection.CELL_2G:
case Connection.CELL_3G:
case Connection.CELL_4G:
case Connection.CELL:
console.log(Connection.WIFI);
console.log(networkConnection.type);
isConnected = true;
break;
}
return isConnected;
});
}
};
}]);
myApp.controller('AppCtrl', function(CordovaNetwork){
CordovaNetwork.isOnline().then(function(isConnected) {
alert(isConnected);
}).catch(function(err){
console.log(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment