Skip to content

Instantly share code, notes, and snippets.

@apla
Created January 9, 2014 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save apla/8332894 to your computer and use it in GitHub Desktop.
Save apla/8332894 to your computer and use it in GitHub Desktop.

Application preferences Cordova plugin.

Store and fetch application preferences using platform facilities. Compatible with phonegap 3.x

Installing:

From plugin registry:

$ cordova plugin add me.apla.cordova.app-preferences

From the repo:

$ cordova plugin add https://github.com/apla/me.apla.cordova.app-preferences

From a local clone:

$ cordova plugin add /path/to/me.apla.cordova.app-preferences/folder

More information: Command-line Interface Guide.

Using Plugman to Manage Plugins.

Synopsis:

function ok (value) {}
function fail (error) {}


var prefs = plugins.appPreferences;

// store key => value pair
prefs.store (ok, fail, 'key', 'value');

// store key => value pair in dict (see notes)
prefs.store (ok, fail, 'dict', 'key', 'value');

// fetch value by key (value will be delivered through "ok" callback)
prefs.fetch (ok, fail, 'key');

// fetch value by key from dict (see notes)
prefs.fetch (ok, fail, 'dict', 'key');

Platforms:

  1. Native execution on iOS using NSUserDefaults
  2. Native execution on Android using android.content.SharedPreferences
  3. Native execution on Windows Phone using IsolatedStorageSettings.ApplicationSettings
  4. (WIP) fallback using localStorage

Notes:

  1. iOS and Android basic values (string, number, boolean) stored using typed fields.
  2. Windows Phone basic values (string, number, boolean) stored using JSON notation.
  3. Complex values, such as arrays and objects, always stored using JSON notation.
  4. Dictionaries supported only on iOS, so on another platforms when using dict key will be written like <dict>.<key>

Tests:

Tests available in src/test.js. After installing plugin, you can add test code from this file and then launch testPlugin() function.

Android and iOS tested ok at this moment.

Credits:

originally ported from: https://github.com/phonegap/phonegap-plugins/tree/master/iOS/ApplicationPreferences

another android implementation: https://github.com/macdonst/AppPreferences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment