Cordova not available when using $stateChangeStart

When using the following code, window.cordova is not (yet) available… Its only reachable after platform.ready.

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

  .run(function ($ionicPlatform, $rootScope, $state) {

    $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 && window.cordova.plugins.Keyboard) {
        window.cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        window.cordova.plugins.Keyboard.disableScroll(true);

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

    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {

      if (toState.name !== 'login') {

        console.log(window.cordova.plugins); //!!!! THIS GOES WRONG !!!!
        /*
         var prefs = plugins.appPreferences;
         prefs.fetch('iuserid').then(function (value) {
         if (value != '') {
         $state.go('login');
         }
         });
         */

        var loggedIn = false;

        if (loggedIn === false) {

          event.preventDefault();

          $state.transitionTo('login');
        }
      }
    });
  })

Any experience with this problem??

Many thanks!

maybe the cordova platform is not ready at the state your run-block is executed.
Because of that every usage of the cordova api is wrappted in:

$ionicPlatform.ready(function () { .... });

Yes I understand that… Thats why the only line of text in my post says, ‘Its only reachable after platform.ready’…

Problem is, the ‘root’ state is triggered before $ready, what means I cannot check something in the local database BEFORE the first state is triggered…

maybe you should take a look in ui-router’s resolve-functionality:

There you can execute code before you enter the state --> you could add a base state for all your authorized states. There you can wrap the content in $ionicPlatform.ready and thats it.