Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. var myApp = angular.module('myApp').service('CordovaNetwork', ['$ionicPlatform', '$q', function($ionicPlatform, $q) {
  2. // Get Cordova's global Connection object or emulate a smilar one
  3. var Connection = window.Connection || {
  4. "CELL" : "cellular",
  5. "CELL_2G" : "2g",
  6. "CELL_3G" : "3g",
  7. "CELL_4G" : "4g",
  8. "ETHERNET" : "ethernet",
  9. "NONE" : "none",
  10. "UNKNOWN" : "unknown",
  11. "WIFI" : "wifi"
  12. };
  13.  
  14. var asyncGetConnection = function () {
  15. var q = $q.defer();
  16. $ionicPlatform.ready(function () {
  17. if(navigator.connection) {
  18. q.resolve(navigator.connection);
  19. } else {
  20. q.reject('navigator.connection is not defined');
  21. }
  22. });
  23. return q.promise;
  24. };
  25.  
  26. return {
  27. isOnline: function () {
  28. return asyncGetConnection().then(function(networkConnection) {
  29. var isConnected = false;
  30.  
  31. switch (networkConnection.type) {
  32. case Connection.ETHERNET:
  33. case Connection.WIFI:
  34. case Connection.CELL_2G:
  35. case Connection.CELL_3G:
  36. case Connection.CELL_4G:
  37. case Connection.CELL:
  38. isConnected = true;
  39. break;
  40. }
  41. return isConnected;
  42. });
  43. }
  44. };
  45. }]);
  46.  
  47.  
  48.  
  49. myApp.controller('AppCtrl', function(CordovaNetwork){
  50. CordovaNetwork.isOnline().then(function(isConnected) {
  51. alert(isConnected);
  52. }).catch(function(err){
  53. console.log(err);
  54. });
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement