Search code examples
angularjscordovaionic-frameworkngcordova

Get network state in ionic app


I'm tying to get the network state in a controller and navigate to s state, when the user is offline.

I've installed ng-cordova.

This is my code:

.controller('MapCtrl', function($scope, $state, $rootScope, $cordovaNetwork) {

  $rootScope.$on('$cordovaNetwork:offline', function() {
    $state.go("error");
    alert("offline")
  });
  $rootScope.$on('$cordovaNetwork:online', function() {
    alert("online")
  });
})

I don't get the alert when I'm online and offline. What is my mistake?


Solution

  • Use Cordova plugin add cordova-plugin-network-information Then window.Connection for Checking available internet.

    if(window.Connection){
        if(navigator.connection.type == Connection.NONE) {
          $state.go("error");
            alert("offline")
           }else{
                console.log(navigator.connection.type);
                  alert("online")
            }
        }