Ionic v1 Network Information Plugin - isOnline() Always false even when Online

#1

First time poster, looking for a genuine and honest answer!

I’m experiencing a weird issue with regards to the cordova-plugin-network-information in conjunction with ngCordova’s “$cordovaNetwork” in that the “isOnline()” check never seems to fire off once the Ionic app’s network state has changed from offline to online.

I’ve looked all over the internet for a fix, but there appears to be nothing that works. Here is my code below:

app.run(function($ionicPlatform, $rootScope, $cordovaSQLite, $cordovaNetwork, $cordovaAppVersion, $cordovaFileTransfer, $localStorage, _, DatabaseService, BackgroundService, ConnectionService) {$ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
        cordova.plugins.Keyboard.disableScroll(true);
    }

    if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
    }

});

$scope.login = function() {
    console.log('Login Function:');

    $scope.loginStarted = true;
    $scope.loginFailed = false;

    console.log($scope.loginData.username);

    if (!angular.isDefined($scope.loginData.username) || !angular.isDefined($scope.loginData.password)) {
        $scope.loginStarted = false;
        $scope.loginFailed = true;
        $scope.failureMessage = 'Please enter both your Login Number and Password.';
    } else {
        var user = {
            login_number: $scope.loginData.username,
            password: $scope.loginData.password
        };



       ConnectionService.getConnection()
             .then(function(conn) {
                 if (conn === 'online') {
                     console.log('ONLINE');
                     $scope.loginStarted = false;
                 } else {
                     console.log('OFFLINE');
                     $scope.loginStarted = false;
                 }
             });


    }
};
app.service('ConnectionService', function($http, $q, $cordovaNetwork, $rootScope) {
var service = {};

service.getConnection = function() {
    var deferred = $q.defer();

    var isOnline;

    var network = $cordovaNetwork.getNetwork();
    isOnline = $cordovaNetwork.isOnline();
    isOffline = $cordovaNetwork.isOffline();

    switch(isOnline) {
        case true:
            console.log('ISONLINE IS ONLINE.');
            deferred.resolve('online');
            break;
        case false:
            console.log('ISONLINE IS OFFLINE.');
            deferred.resolve('offline');
            break;
        default:
            console.log('DEFAULT TRIGGERED');
            deferred.resolve('offline');
            break;
    }

    return deferred.promise;
};

return service;
});

Before I log in, I’m testing to see if there is an Online connection. Initially, my device is Online. I then test with the device Offline, then switch back to Online. The isOnline() should trigger and be Online, however, this never appears to be the case, so when the app returns Online, it isn’t picking this up, and so when I do eventually call my API, (omitted from the code samples), it can never reach it.

Any help would be much appreciated.

#2

What version of the cordova-plugin-network-information are you using? I would recommend using version 1.3.3 with an Ionic v1 app.