0

I need to save some simple seed data to generate some user-saved info when my PhoneGap app is launched, so all I need is a simple array or string to be saved to the device. I am lazy and I am using PhoneGapBuild rather than setting up PhoneGap locally.

The problem with this approach is that I can't modify the source files that would otherwise grant access to cookies functionality and filesystem stuff.

I'm hoping there is a simple way to store a bit of data (I have looked at cookies, filesystem access and a possible database plugin) that I have overlooked, and that will compile with PGB.

And does filesystem access even natively work with PGB? I haven't been able to get it going but I haven't really tried hard.

Abraham Brookes
  • 1,720
  • 1
  • 17
  • 32

1 Answers1

0

I had overlooked something. The localStorage object. It stores data locally and can be accessed only by the browser that saved it. It is a html5 feature and luckily it works on Apache Cordova - the PhoneGap browser/wrapper. You use them as such:

localStorage.myData = 'string';
// or
localStorage['myData'] = 'string';
// or
localStorage.setItem('myData', 'string');

and to return

localStorage.myData;
// or
localStorage['myData'];
//or
localStorage.getItem('myData');

WAY easier to use than file API, and if you want to store arrays, use

localStorage.myArray = JSON.stringify(array);

and to return

JSON.parse(localStorage.myArray);

Many thanks to How do I store an array in localStorage?

Community
  • 1
  • 1
Abraham Brookes
  • 1,720
  • 1
  • 17
  • 32