localStorage works in browser but not on android and ios


#1

I want to show an intro the first time the app is launched. I am setting a boolean property on the localStorage in the .run() method, and it works ok in the desktop browser, but doesn’t work on Android (real device and emulator) and IOS (emulator). By doesn’t work I mean that the state 'app.intro' is never called on app startup.

This is the code in app.js:

angular.module('starter', ['ionic', 'starter.controllers'])
  .run(function ($ionicPlatform, $state) {
    $ionicPlatform.ready(function () {
      if (window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);

      }
      if (window.StatusBar) {
        StatusBar.styleDefault();
      }

      if (window.localStorage && !window.localStorage.getItem('firstRunDone')) {
        window.localStorage.setItem('firstRunDone', true);
        $state.go('app.intro');
      }
    });
  })

Is there something I need to enable in the platform configurations in order for localStorage to work?

This is the output of ionic info:

Gulp version:  CLI version 3.9.1
Gulp local:   Local version 3.9.1
Ionic Framework Version: 1.2.4
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
ios-deploy version: 1.8.5 
ios-sim version: 5.0.8 
OS: Mac OS X Yosemite
Node Version: v5.10.1
Xcode version: Xcode 7.2.1 Build version 7C1002 

Building the project on linux made no difference.


#2

Looks like the problem was caused by updating ionic and dependencies while the files in platforms got out of sync.

The solution was to do:

cordova platform rm ios
cordova platform rm android
cordova platform add ios --save
cordova platform add android --save

After re-adding the platforms and rebuilding localStorage started working as expected.


#3

How to rebuild localStorage