Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Native storage of variables in cordova Android / iOS apps.

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 1.90k
    Comment on it

    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)

  • Hi,
    You need to add plugins version also.
    Try:

    <plugin name="cordova-plugin-nativestorage" source="npm" spec="~2.1.0"/>
    

    It will make executable file for both ( Android and iOS).

    Hope this will help you :)

  • Hi ,thank you for very useful topic. i try to compile my code via phonegap. i add to config.xml file row

    plugin name="cordova-plugin-nativestorage" source="npm"

    but phonegap make exequtable file only for Android. is for iOS i need add something else?

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: