How persistent is local storage on Android and iOS?

When my application stores data locally on the phone, how permanent is this storage? I will clarify the exact situation:

I am building an application with jQueryMobile and Phonegap. This is essentially a browser app, but using Phonegap allows me to pack and sell it in app stores, among other benefits.

Phonegap offers two storage methods, both of which harmonize the native functions of iOs, Android, Blackberry and some other OSs: localStorage (these are primitive key-value pairs) and a Web SQL database. Both localStorage and web SQL are browser-owned storage forms. Having said that, I canโ€™t find out how long the data will be stored, under what circumstances it will be deleted, under what circumstances it may be unavailable, etc.

For example, if an application stores data with localStorage or web SQL, and the user switches to another standard browser on their Android, the application will open with a new browser and does this mean that the stored data is not available?

If the user does not use the application for a year (which in my case is a realistic and not necessarily a bad scenario), will the data expire like cookies, or could a flood of data from other applications be pushed out of the browser storage?

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

Or localStorage and web SQL - the kind of storage that you delete only when (in Android) you go to "Settings"> "Applications" and actively delete user data associated with the application?

Thanks for any ideas. There is nothing informative on ol 'www.

What happens if the application is updated. Will local and web storage be deleted or will it remain?

+70
local-storage cordova web-sql
Oct 13 2018-11-11T00:
source share
8 answers

Let me answer step by step

if the application stores data with localStorage or web SQL, and the user switches to another standard browser on Android, will the application be opened with a new browser, and this means that the saved data is inaccessible?

The data is stored in the cache (this is not really the cache) of the browser, so if you change the browser or set the settings to delete or change the default browser, the data will go.

If the user does not use the application for a year (which in my case is a realistic and not necessarily a bad scenario), will the data expire like a cookie, or maybe it was pushed out of the storage browser by flood of data from other applications?

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

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

No, the data remains valid. :-)

Or localStorage and web SQL - the kind of storage that you only delete when (in Android) you go to "Settings"> "Applications" and actively delete user data associated with the application?

Yes, data is only displayed if you manually deleted it from your application or deleted the application. He will remain in all other cases.

EDIT: In the case of iOS, the OS will delete the data in local storage when there is not enough memory in the device.

+56
Oct 13 '11 at 12:51
source share

In response to iOS 5.1 @ghostCoder, the answer is no longer valid. Apple decided to move the local storage to the cache folder, which can be emptied at any time. You can track this discussion here:

Discussion of Google Groups in Localstorage

This blog also explains the problem in more detail:

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

You can manually specify the location of the local store in the safe location Application_Home> / Documents . To determine the current local storage 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 a different location for your local storage:

 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]; } 
+35
Mar 22 '12 at 10:18
source share

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

He installed, installed and received functions that implement the platform's features, such as the general android and iOS NSUserDefaults settings, which make data storage as safe as possible.

 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>); 
+5
Apr 20 '16 at 8:07
source share

In android, this is the default persistent storage. Even if the user updates the application, he remains the same.

The user can go to the settings and clear the cache and data, in which case he will work, or, if he says that this application does for clean applications.

Even on iOS, this is persistent storage, but does not know about the application update script. But in the older version (5.1) this was not so, and in 6+ they made it possible to make it permanent through the flag, which was quickly turned on with cordova / phonegap.

+3
Mar 21 '16 at 6:57
source share

The good solution that is available now is the Cordova Root Drive .

It allows you to use a simple but natural robust method to save data on iOS and Android by naturally embedding SharedPreferences on Android and NSDefault on iOS to ensure reliability.

Using:

Installation:

 cordova plugin add cordova-plugin-nativestorage 

Saving Values:

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

Getting Values:

 NativeStorage.getItem("reference_to_value",<success-callback>, <error-callback>); 
+3
Aug 23 '16 at 9:34
source share

I can't speak on other platforms, but on my Android device 4.1 I use localStorage to store some data locally using jQuery Mobile, and I found that Android will clear my cache every week or so without my knowledge. I am going to give Web SQL a try and see which is better.

+1
Dec 13
source share

I found this online ng-persist

Store data on mobile devices (using the cord) that are saved even if the user reinstalls the application

Installation

bower install ng-persist ngstorage --save

Paste $persist into the controller

 .controller('MyCtrl', function($persist) { $persist .set(namespace, key, val) .then(function () { // saved }); // read $persist .get(namespace, key, fallback) .then(function (val) { // val is either the value, if exists, or the fallback }); // delete $persist .remove(namespace, key) .then(function () { // removed }); }); 
0
Feb 19 '16 at 12:37
source share

It would be better in terms of performance, security and reliability of AVOID to use local storage and use Cordova-sqlite-storage .

Some notes:

localStorage has the advantage of being easy to use but working synchronously. This means that it can block the user interface thread and cause poor performance.

localStorage has a limited amount of storage, usually 5 MB.

localStorage can be destroyed by OS (android, ios) at any time. This is really serious if you care about data security.

On the other hand:

Cordova-sqlite-storage is asynchronous and does not block the user interface stream, it saves your data on the deviceโ€™s own media, the storage capacity is not limited, the OS will not delete any data if you do not delete the application.

0
May 27 '17 at 12:23
source share



All Articles