简体   繁体   中英

What is the Best way to check internet connection in Ionic

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 ??

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");
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM