Html – iOS 7 webview and localStorage persistence

cordovahtmliosiphonelocal-storage

I'm developing a hybrid app (for iOS and Android only) using PhoneGap/Cordova and want to use HTML5 localStorage to store content for offline access.

http://caniuse.com/#search=localStorage says – "In iOS 5 & 6 localStorage data is stored in a location that may occasionally be cleared out by the OS."

  1. What is the situation with an iOS 7 (and later) webview, in what cases will localStorage persist, or get cleared out (by the OS, or the user)?

  2. Will an update to the app clear localStorage?

  3. What about the user clearing browser history on Safari – will that apply to the webview too?

  4. Do I need to worry (or can I even control) where on the fils system the localStorage is created. I understand it should not be backed up on iCloud.


I got a device (iPad) and checking the file system I see that localStorage file is in ~/Library/Caches within the app sandbox, see image below.

From the docs:

https://developer.apple.com/icloud/documentation/data-storage/index.html
Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.

I am simply doing this to set data:

  localStorage.setItem('foo','this is the FOO value');
  localStorage.setItem('bar','and this is the BAR value');

enter image description here

Best Solution

If you do still face the issue with Cordova-iOS v4 , then try the NativeStorage plugin. https://www.npmjs.com/package/cordova-plugin-nativestorage.

It has set, put and get functions which implement platform capabilities like android shared preferences and iOS NSUserDefaults which makes data store as safe as allowed.

cordova plugin add cordova-plugin-nativestorage

NativeStorage.putObject("reference_to_value",<object>, <success-callback>, <error-callback>);

NativeStorage.getObject("reference_to_value",<success-callback>, <error-callback>);