Question

I'm using AngularJS v1.2.7 with Cordova 3.3.0 (Android) I need to store some settings in the phone memory- so when the app/device restarts, the app can still have access to this stored data.

Any tutorials about this? I couldn't find any :(

Knowing that Cordova supports; LocalStorage, WebSQL (no more maintained), IndexedDB (not widely supported by Opera and Safari). I tend to use LocalStorage but does it keep/remember the data even after I restart the device?

At the moment I'm using the answer #12969480 by Richard Szalay .

Was it helpful?

Solution 2

I would suggest writing a custom plugin and store the settings properly in the Objective-C user defaults.

LocalStorage is not permanent storage, and WebSQL seems like overkill for storing settings.

You don't have to get too involved in Objective-C and there are good Phonegap/Cordova plugin guides:

http://docs.phonegap.com/en/3.3.0/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide

I use this code to store a "FirstRun" variable to check if the app is a new install. The plugin checks if the app has been run before, and returns 1 or 0, which is parsed to an integer and evaluated as true/false.

You could update this code and add more methods to the "AppChecks" class. I put all simple checks and setting storage in this class.

AppChecks.h

#import <Cordova/CDVPlugin.h>

@interface AppChecks : CDVPlugin

- (void) checkFirstRun:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end

AppChecks.m

#import "AppChecks.h"
#import <Cordova/CDVPluginResult.h>

@implementation AppChecks

- (void) checkFirstRun:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];

CDVPluginResult* pluginResult = nil;
NSString* javaScript = nil;

@try {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isFirstRun = @"1";

    if (![defaults objectForKey:@"firstRun"]) {
        [defaults setObject:[NSDate date] forKey:@"firstRun"];
    } else {
        isFirstRun = @"0";
    }

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isFirstRun];
    javaScript = [pluginResult toSuccessCallbackString:callbackId];
} @catch (NSException* exception) {
    // could not get locale
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
    javaScript = [pluginResult toErrorCallbackString:callbackId];
}
[self writeJavascript:javaScript];
}
@end

Used in my Javascript code:

cordova.exec(
    function( isFirstRun ) {
        isFirstRun = parseInt( isFirstRun );
        if( isFirstRun ) {
          // do stuff for first run
        }
    },
    function(err) {
        // handle error from plugin
    },
    "AppChecks",
    "checkFirstRun",
    []
);

OTHER TIPS

I was looking around for something similar for the hell of it. I found the following:

http://ngmodules.org/modules/angularLocalStorage

Example has a text box showing the saved state: http://plnkr.co/edit/Y1mrNVRkInCItqvZXtto?p=preview

var eS = angular.module('exampleStore', ['localStorage']);

  eS.controller('MainCtrl',['$scope','$store',function($scope, $store) {
      $store.bind($scope, 'propertyOnScope', 'My default value');

      $scope.clearTest = function(){ 
          $store.remove('propertyOnScope'); 
      };
  }]);

LocalStorage is translated to the native file system with Cordova, so in fact the data will persist for the life of the installation.

Check out ngCordova from the Drifty guys who created Ionic.

These AngularJS Cordova wrappers make life just a little bit easier, if you're someone who wants to access native device functionality through Cordova, using AngularJS as your front end.

Also, check out their new web series, the Ionic Show. They talk about current, relevant tools for hybrid mobile app developers.

A basic tutorial for using localStorage with phonegap can be found here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top