I have an ionic project that uses localStorage
, is working for months but after I added crosswalk the localStorage
is not working anymore. The strange thing is, I create another app with ionic start todo-list sidemenu
and created a simple todo-list using localStorage
and worked.
Probably the issue is with something in my another app combined with crosswalk that is conflicting and ruining the localStorage
.
I know what I said is to vague but maybe someone could had this same issue.
At this link:
Crosswalk WebView stores data (IndexedDB, LocalStorage, etc) separately from System WebView
I'm using like this:
window.localStorage.setItem('foo', 'bar');
window.localStorage.getItem('foo');
The example above, the getItem
will display the bar
value but if I close the app and do getItem('foo')
in another part of the app the value return null
.
The issue is just happening on Android devices (tested 4.1, 4.3 and 4.4), at web browser is working.
For the record, that is the plugins installed at the project:
com.ionic.keyboard 1.0.4 "Keyboard"
com.phonegap.plugins.PushPlugin 2.4.0 "PushPlugin"
cordova-plugin-console 1.0.0 "Console"
cordova-plugin-crosswalk-webview 1.2.0 "Crosswalk WebView Engine"
cordova-plugin-dialogs 1.1.0 "Notification"
cordova-plugin-geolocation 1.0.0 "Geolocation"
cordova-plugin-inappbrowser 1.0.0 "InAppBrowser"
cordova-plugin-network-information 1.0.0 "Network Information"
cordova-plugin-splashscreen 2.0.0 "Splashscreen"
cordova-plugin-statusbar 1.0.0 "StatusBar"
cordova-plugin-vibration 1.1.0 "Vibration"
cordova-plugin-whitelist 1.0.0 "Whitelist"
nl.x-services.plugins.toast 2.0.5 "Toast"
I had the same problem (using cordova-android 4.0 and Crosswalk 13.42.319.11). Before adding Crosswalk, localStorage worked as expected. After adding Crosswalk, any new items added to local storage would not be persisted if the application unloaded and restarted.
Through a process of elimination I found the following code in our application that appeared to trigger the problem:
// The purpose of this code is to check whether Safari browser is in "Private" browsing mode,
// in which case the localStorage API is available but throws an exception when setItem() is called:
// "QuotaExceededError: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota."
function testLocalStorageIsSupported() {
try {
if (window.localStorage && window.localStorage.setItem && window.localStorage.getItem && window.localStorage.removeItem) {
// Store an arbitrary value
var storedValue = (new Date().valueOf()).toString();
window.localStorage.setItem("__LocalStorageIsSupported__", storedValue);
var loadedValue = window.localStorage.getItem("__LocalStorageIsSupported__");
window.localStorage.removeItem("__LocalStorageIsSupported__");
// If loaded value matches the stored value then consider localStorage to be supported.
return (storedValue == loadedValue);
}
return false;
} catch (e) {
console.log("LocalStorage: Caught an error testing availability of localstorage: " + e);
return false;
}
}
This code was being called after the deviceready event is received. Removing this code from the application solved the problem for me.
I reduced the problem code to the one line:
window.localStorage.setItem("__LocalStorageIsSupported__", (new Date().valueOf()).toString())
I did some further testing, by creating a blank cordova-android 4.0 application, and adding Crosswalk. Initially localStorage works as it should. After adding the above single line of code into the callback for the deviceready event, localStorage stopped persisting.