Ionic v1网络信息插件-isOnline()即使在线也始终为false

时间:2019-01-21 14:46:22

标签: android angularjs ionic-framework ngcordova ionic-v1

第一次张贴海报,寻找真实,诚实的答案!

我在与ngCordova的“ $ cordovaNetwork”结合使用cordova-plugin-network-information时遇到了一个奇怪的问题,因为一旦Ionic应用程序的网络状态变为“ isOnline()”检查似乎就不会启动从离线更改为在线。

我已经在互联网上寻找解决方法,但是似乎没有任何解决方法。这是我的代码如下:

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

在登录之前,我正在测试是否存在在线连接。最初,我的设备处于在线状态。然后,我使用脱机设备进行测试,然后切换回在线状态。 isOnline()应该触发并处于在线状态,但是这种情况似乎从来没有发生过,因此当应用返回在线状态时,它并没有采取这种措施,因此当我最终调用我的API时(从代码中省略)样本),它永远无法达到目标。

任何帮助将不胜感激。

0 个答案:

没有答案