3

Right Now I am Using this code to check the internet connection. And it only checks when it is triggered or called:

.factory('Checkinternet', function($ionicPlatform,$ionicPopup) { 
  return {
      isonline: function() {
         if(window.Connection) {
              if(navigator.connection.type == Connection.NONE){
                    $ionicPopup.confirm({
                      title: 'No Internet Connection',
                      content: '<span style="color:black">Sorry, no Internet connectivity detected. Please reconnect and try again.</span>'
                    }).then(function(result) {
                      if(!result) {
                        //ionic.Platform.exitApp();
                      }
                    });
                    return 'off';
              }
          }
      }
  };
})

What I want is whenever Internet goes away It should Alert (Without Using Setinterval as it is slowing the performance of my app when it runs in background)

Is there any other way Of doing this ??

MordechayS
  • 1,552
  • 2
  • 19
  • 29
  • you can use http interceptors – xkeshav Nov 02 '16 at 11:06
  • 1
    Possible duplicate of [Trying to rewrite controller as service, so that it can check network status all over app](http://stackoverflow.com/questions/40058831/trying-to-rewrite-controller-as-service-so-that-it-can-check-network-status-all) – Heretic Monkey Nov 02 '16 at 14:03

1 Answers1

1

Please check my other post: Trying to rewrite controller as service, so that it can check network status all over app.

You can check your internet connection anywhere you want by simply add:

if(ConnectivityMonitor.isOnline()){

   //do something

}else{

  //do something else 
  alert("No internet connection");
}   
Community
  • 1
  • 1
e7lT2P
  • 1,635
  • 5
  • 31
  • 57