16

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

sandstrom
  • 14,554
  • 7
  • 65
  • 62
KevInSol
  • 2,560
  • 4
  • 32
  • 46

3 Answers3

4

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>);
1

If you are using cordova, that problem was fixed long time ago, even for iOS 5 and iOS 6. You shouldn't worry if you are using the latest version of cordova.

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 1
    Hi, thank you - do you have a link to further information please? Can I just do localStorage.setItem('foo','FOO value'); or do I need a plugin etc? – KevInSol Sep 03 '14 at 14:09
  • I have no links, I just follow cordova development and read this long time ago. it should be on the cordova or phonegap blog, where they publish each version changes, but I don't remember the version where this was fixed. Or you can try a search on the cordova jira – jcesarmobile Sep 03 '14 at 22:27
  • 1
    I don't have a link, but I *have* looked through the code several times, and I can verify that this issue is fixed (and has been for quite some time). Use localStorage at your pleasure. (If you're targeting WP7/8, I'd stay with `setItem`, but otherwise you can reference it using `localStorage.foo="FOO value"`). Do keep in mind there is a size limit here, so don't go *too* crazy. Also, clearing Mobile Safari's cache won't impact your app at all (sandbox), and it will also persist across an update (but *not* a delete + reinstall). – Kerri Shotts Sep 03 '14 at 22:37
  • jcesarmobile - thanks, I'll have a look at the jira and blogs when I have some free time. Re the docs, this has lots of detail on localstorage - https://cordova.apache.org/docs/en/3.0.0/cordova_storage_storage.md.html#localStorage, but for the current version, http://cordova.apache.org/docs/en/3.5.0/cordova_storage_storage.md.html#Storage it just refers you to the W3C spec. could i use the 3.0 guide? – KevInSol Sep 04 '14 at 06:44
  • Kerri - thanks for your response. Following on from the above comment i made re the docs, i have not seen your localStorage.foo="FOO value" syntax before. Just so I am consistent when doing web work, could I use setItem etc in Android/iOS and still avail of the fix? Or will the fix only work with your syntax? – KevInSol Sep 04 '14 at 06:50
  • Kerri, yes I've seen the size limit reported as little as 2mb for andriod, and that JavaScript uses UTF-16, so that means max 1m chars. I need about 25-50% of that. – KevInSol Sep 04 '14 at 06:56
  • Why "if you are using cordova"? Does it get stored somewhere different than using local storage in a regular web app would? I'm dealing with this issue for a regular HTML5 web page now - trying to figure out why my storage randomly got cleared (on iOS 7.1.2, in mobile Safari). – Nicole Stein Sep 07 '14 at 01:54
  • If you use Cordova the localStorage is stored in it's own sandbox as Kerri mentioned in a previous comment, it's different from mobile safari sandbox – jcesarmobile Sep 07 '14 at 09:31
  • I don't think that this is true. I have seen iOS wipe my localStorage and I am working with Cordova 6.5. ios 5 & 6. – user1261710 Oct 06 '17 at 15:22
  • @user1261710 it was true by the time I wrote it, there was code in Cordova to prevent the data loss. But a lot of nev versions have been released since then and the code might have been removed. Also, you say you use? Cordova 6.5 on iOS 5 and iOS 6? Those iOS versions are not supported anymore – jcesarmobile Oct 06 '17 at 16:21
1

Your best bet is to use the NativeStorage plugin:

https://www.npmjs.com/package/cordova-plugin-nativestorage

To answer your questions:

  1. The localStorage data is kept in a cache directory in the filesystem, and cache is cleaned by the OS frequently (for example when the device is low on disk space).
  2. No, updating the app won't clear local storage.
  3. No, Safari local storage and Webview local storage are separate.
  4. No, you cannot control where on the filesystem it's stored.
sandstrom
  • 14,554
  • 7
  • 65
  • 62