How permanent is local storage on Android and iOS?

Let me answer step by step

if the app stores data with localStorage or web SQL, and the user switches to a different standard browser on their Android, will the app be opened with the new browser and does that mean that the stored data is unavailable?

The data is saved in the 'cache'(its not exactly the cache) of the browser, so if you change the browser, or set the settings so that the default browser is removed or changed, the data will go.

If the user doesn't use the app for a year (which in my case is a realistic and not necessarily a bad scenario), will the data have expired like a cookie, or maybe been pushed out of the browser's storage by a deluge of data from other apps?

No, the data will stay there no matter for how long it is not used. So even if you clear the browser cache, it will still be there.

Or will the data be destroyed even earlier, such as when: - user visits another site in the browser - browser is manually closed - browser process is killed or dies - etc

No, the data stays all right. :-)

Or are localStorage and web SQL the kind of storage that you only delete when (in Android) you go to Settings > Apps and actively remove the user data associated with the app?

Yes, the data only goes if you either manually delete it from your app or you uninstall your app. It will stay in all other cases.

EDIT: In the case of iOS, the OS will remove the data in the local storage when there is a shortage of memory in the device.


As of iOS 5.1 @ghostCoder's answer is no longer valid. Apple has decided to move the localstorage location into a cache folder which can be emptied anytime. You can track this discussion here:

Google Groups Discussion on Localstorage

Also this blog explains the problem in more detail:

http://www.marco.org/2011/10/13/ios5-caches-cleaning

It is possible to manually point your localstorage location to a safe Application_Home>/Documents location. To determine your current localstorage location you can use something like this:

NSString* localStorageSubdir = (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/LocalStorage";
NSString* localStoragePath = [library stringByAppendingPathComponent:localStorageSubdir];
NSString* localStorageDb = [localStoragePath stringByAppendingPathComponent:@"file__0.localstorage"];

The following code allows you to set another location for your localstorage:

NSString* bundleIdentifier = [[mainBundle infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString* libraryPreferences = @"Library/Preferences";
NSString* appPlistPath = [[bundlePath stringByAppendingPathComponent:libraryPreferences]    stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", bundleIdentifier]];

NSString *value;
NSString *key = @"WebKitLocalStorageDatabasePathPreferenceKey";
value = [appPlistDict objectForKey: key];
if (![value isEqual:ourLocalStoragePath]) {
    [appPlistDict setValue:yourPreferredLocalStoragePath forKey:key];
} 

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

A good solution which is available now is The Cordova Native Storage Plugin.

It allows a simple yet native persistant method to save data in iOS and Android by natively implementing SharedPreferences in Android and NSDefault in iOS to guarantee reliability.

Usage:

Installation:

cordova plugin add cordova-plugin-nativestorage

Storing values:

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

Retrieving values:

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