Search code examples
javascriptcordovaphonegap

How to save data in Cordova apps using local storage


I'm making a question and answer app, how can I use local storage to save the the number the user stopped at, so he/she will resume from there when next he/she opens the app?


Solution

  • A simple solution would be to use https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

    This stores key value pairs on the window object that will persist throughout app sessions. Bear in mind though:

    • Devices can randomly delete this weak storage method if storage device is running low
    • If you want to store objects / arrays you'll need to stringify and parse them before storing and after retrieving with JSON.stringify and JSON.parse

    See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse