Search code examples
javascriptxmlnativescript

Is there localstorage in nativescript?


How to keep data in a NativeScript application persistent. Can anyone tell about localStorage in NativeScript?

Edit: Was looking for localStorage at the time.


Solution

  • You can use either with global.foo, it will available for whole app or you can use application-settings module

    var applicationSettings = require("application-settings");
    //set somewhere like this
    applicationSettings.set<Number|String|Boolean>("sharedVar",1);
    
    //get sharedVar somewhere
    applicationSettings.get<Number|String|Boolean>("sharedVar");//if empty it will be null
    
    //or if u want default value if sharedVar wasn't defined
    //and if sharedVar was defined then u will get content of sharedVar
    applicationSettings.get<Number|String|Boolean>("sharedVar","Default Value");
    

    DOCS:

    https://docs.nativescript.org/cookbook/application-settings

    https://docs.nativescript.org/api-reference/modules/_application_settings_.html

    EDIT: had typo not globals but global :D

    EDIT2: change names of function from applicationSettings and link for docs