Angularjs localStorage在cordova应用程序中存储设置

时间:2022-08-24 12:16:40

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.

我正在使用AngularJS v1.2.7和Cordova 3.3.0(Android)我需要在手机内存中存储一​​些设置 - 所以当应用程序/设备重新启动时,应用程序仍然可以访问这些存储的数据。

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?

知道Cordova支持; LocalStorage,WebSQL(不再维护),IndexedDB(Opera和Safari没有广泛支持)。我倾向于使用LocalStorage,但是即使在我重启设备后它仍保留/记住数据?

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

目前我正在使用Richard Szalay的答案#12969480。

3 个解决方案

#1


1  

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

我建议编写一个自定义插件,并在Objective-C用户默认值中正确存储设置。

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

LocalStorage不是永久存储,而WebSQL对于存储设置似乎有些过分。

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

您不必过多参与Objective-C并且有很好的Phonegap / Cordova插件指南:

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.

我使用此代码存储“FirstRun”变量来检查应用程序是否是新安装。插件检查应用程序之前是否已运行,并返回1或0,将其解析为整数并计算为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”类添加更多方法。我把所有简单的检查和设置存储放在这个类中。

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:

在我的Javascript代码中使用:

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

#2


2  

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

示例有一个显示已保存状态的文本框: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'); 
      };
  }]);

#3


0  

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

LocalStorage已转换为Cordova的本机文件系统,因此实际上数据将在安装的整个生命周期中持续存在。

Check out ngCordova from the Drifty guys who created Ionic.

查看创建Ionic的Drifty人员的ngCordova。

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.

如果您想通过Cordova访问本机设备功能,使用AngularJS作为您的前端,这些AngularJS Cordova包装器可以让生活更轻松一些。

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

另外,看看他们的新网络系列,Ionic Show。他们讨论了混合移动应用程序开发人员当前的相关工具。

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

可以在此处找到使用localStorage和phonegap的基本教程

#1


1  

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

我建议编写一个自定义插件,并在Objective-C用户默认值中正确存储设置。

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

LocalStorage不是永久存储,而WebSQL对于存储设置似乎有些过分。

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

您不必过多参与Objective-C并且有很好的Phonegap / Cordova插件指南:

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.

我使用此代码存储“FirstRun”变量来检查应用程序是否是新安装。插件检查应用程序之前是否已运行,并返回1或0,将其解析为整数并计算为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”类添加更多方法。我把所有简单的检查和设置存储放在这个类中。

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:

在我的Javascript代码中使用:

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

#2


2  

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

示例有一个显示已保存状态的文本框: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'); 
      };
  }]);

#3


0  

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

LocalStorage已转换为Cordova的本机文件系统,因此实际上数据将在安装的整个生命周期中持续存在。

Check out ngCordova from the Drifty guys who created Ionic.

查看创建Ionic的Drifty人员的ngCordova。

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.

如果您想通过Cordova访问本机设备功能,使用AngularJS作为您的前端,这些AngularJS Cordova包装器可以让生活更轻松一些。

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

另外,看看他们的新网络系列,Ionic Show。他们讨论了混合移动应用程序开发人员当前的相关工具。

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

可以在此处找到使用localStorage和phonegap的基本教程