Skip to content

Usage API (prior to v2.0.0)

Gilles Callebaut edited this page May 11, 2016 · 1 revision

How can we use this plugin?

##Installation The plugin can be installed via the Cordova command line interface:

  • Navigate to the root folder for your phonegap project.
  • Run the command:
cordova plugin add cordova-plugin-nativestorage

or through this git repo if you want to be running the development version:

cordova plugin add https://github.com/GillesC/cordova-plugin-nativestorage.git

##Storing values The developr can opt to store values through the set or the put<type> method.

NativeStorage.set("reference_to_value",<value>, <success-callback>, <error-callback>);
NativeStorage.put<type>("reference_to_value",<value>, <success-callback>, <error-callback>);
NativeStorage.get<type>("reference_to_value",<success-callback>, <error-callback>);
NativeStorage.remove("reference_to_value",<success-callback>, <error-callback>);

##Retrieving values

NativeStorage.get<type>("reference_to_value",<success-callback>, <error-callback>);

##Removing values

NativeStorage.remove("reference_to_value",<success-callback>, <error-callback>);

##Example per type

Booleans

NativeStorage.putBoolean("reference_to_value",<bool-value>, <success-callback>, <error-callback>);

NativeStorage.getBoolean("reference_to_value",<success-callback>, <error-callback>);

Integers

NativeStorage.putInt("reference_to_value",<int-value>, <success-callback>, <error-callback>);

NativeStorage.getInt("reference_to_value", <success-callback>, <error-callback>);

Doubles

NativeStorage.putDouble("reference_to_value",<double-value>, <success-callback>, <error-callback>);

NativeStorage.getDouble("reference_to_value",<success-callback>, <error-callback>);

Strings

NativeStorage.putString("reference_to_value",<string>, <success-callback>, <error-callback>);

NativeStorage.getString("reference_to_value",<success-callback>, <error-callback>);

Objects

NativeStorage.putObject("reference_to_value",<object>, <success-callback>, <error-callback>);

NativeStorage.getObject("reference_to_value",<success-callback>, <error-callback>);

##Full Example

var app = {
	initialize: function() {
		this.bindEvents();
	},
	bindEvents: function() {
		document.addEventListener('deviceready', this.onDeviceReady, false);
	},
	onDeviceReady: function() {
		app.receivedEvent('deviceready');
	},
	receivedEvent: function(id) {
		NativeStorage.set("ref_bool",true, function(result){
			console.log("Put! "+result);

			NativeStorage.getBoolean("ref_bool", function(result){
				console.log("Got: "+result);
			},
			function(e){
				console.log(e);
			});
		},
		function(e){
			console.log(e);
		});



		NativeStorage.set("ref_int",22, function(result){
			console.log("Put! "+result);

			NativeStorage.getInt("ref_int", function(result){
				console.log("Got: "+result);
			},
			function(e){
				console.log(e);
			});
		},
		function(e){
			console.log(e);
		});



		NativeStorage.set("ref_float",3.14, function(result){
			console.log("Put! "+result);

			NativeStorage.getDouble("ref_float", function(result){
				console.log("Got: "+result);
			},
			function(e){
				console.log(e);
			});
		},
		function(e){
			console.log(e);
		});



		NativeStorage.set("ref_string","hahaha", function(result){
			console.log("Put! "+result);

			NativeStorage.getString("ref_string", function(result){
				console.log("Got: "+result);
				console.log("Now removing it");
				NativeStorage.remove("ref_string", function(){console.log("Removed with success!");}, function(e){console.log(e);})
			},
			function(e){
				console.log(e);
			});
		},
		function(e){
			console.log(e);
		});
	}
};
app.initialize();

##Demo Example A demo application can be found at cordova-plugin-nativestorage/examples/demo. This application will save a String when the SAVE (btn_load) is pushed. This String is the value which has been typed in the input field (data_input). When the LOAD button is pressed, the value is shown by means of an alert message.

Installation

  • Cloning the repo to a local dir
git clone https://github.com/GillesC/cordova-plugin-nativestorage.git
  • Navigating to the demo dir
cd cordova-plugin-nativestorage/examples/demo/
  • Adding platforms you require
cordova platform add ios
cordova platform add android
cordova platform add browser
  • Adding the plugin
cordova plugin add cordova-plugin-nativestorage
  • Run or emulate the demo application
cordova emulate ios
cordova run android
cordova run browser