Ionic Angular JS cookie does not work

advertisements

app.js

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

.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {

if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);

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

});
})

.config(function ($stateProvider, $urlRouterProvider) {

$stateProvider

  .state('HomePage', {
             url: '/HomePage/:id',
             templateUrl: 'templates/HomePage.html',
             controller: 'HomePageCtrl'
         })
         .state('login', {
             url: '/login',
             templateUrl: 'templates/Login.html',
             controller: 'LoginCtrl'
         });
  $urlRouterProvider.otherwise('/login');

});

controllers.js

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

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http, $cookieStore) {

$cookieStore.put('mycookie', 'cookie value');
var favoriteCookie = $cookieStore.get('mycookie');

   var alertPopup = $ionicPopup.alert({
   title: 'favoriteCookie title',
   template: favoriteCookie // This does not work on phone.

});})

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ionic/ng-cordova-master/demo/www/lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
    <script src="lib/ionic/js/cookies.js"></script>

</head>
<body ng-app="starter">

    <ion-nav-view></ion-nav-view>

</body>
</html>

Question:

If i use $cookieStore it works on browser without any problem.

However when i publish project to phone $cookieStore is not working. I mean When i try to use $cookieStore in android phone.

$cookieStore displays below error on $ionicPopup.alert.

undefined

How can i use cookie on android by using ionic?

Any help will be appreciated.

Thanks.


Please vote up this answer instead as I wasn't logged in properly on the other one when I posted >_< doh!

Instead of using $cookieStore which doesn't seem to play nicely on Android, simply replace it with the default localStorage. It works just fine! :)

$cookieStore.set() -> window.localStorage.setItem() $cookieStore.get() -> window.localStorage.getItem() $cookieStore.remove() -> window.localStorage.removeItem()