Hello Readers,
Cordova native storage plugin provides persistence storage in Cordova Android and iOS apps. We use this plugin because localStorage has non-persistent property in WebView of Android and iOS.
Basically this plugin is specially used in iOS app because it removes localStorage value when OS is running out of memory. This plugin supports Android and iOS platform.
To use the native storage functionality first of all we need to install the plugin through CLI, running:
$ cordovapluginaddcordova-plugin-nativestorage
We can store, retrieve and remove the value by using the plugin.
Here is the full example showing full functionality:
var app = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function () {
var obj = "cordovaNativeStorage";
// Setting the value of obj in to a variable.
NativeStorage.setItem("pluginName", obj, this.setSuccess, this.setError);
},
setSuccess: function (obj) {
console.log(obj);
NativeStorage.getItem("pluginName", this.getSuccess, this.getError);
},
setError: function (error) {
if (error.exception !== "") console.log(error.exception);
},
getSuccess: function (obj) {
NativeStorage.remove("pluginName", this.removeSuccess, this.removeError);
},
getError: function (error) {
console.log(error.code);
if (error.exception !== "") console.log(error.exception);
},
removeSuccess: function (obj) {
console.log(obj);
},
removeError: function (error) {
console.log(error.code);
if (error.exception !== "") console.log(error.exception);
}
};
app.initialize();
Note: If you are facing any problem then remove the plugin and re-install it again.
Hope this will help you :)
2 Comment(s)