Share localStorage via webView and CordovaWebView android
I am trying to communicate in activity between webView and CordovaWebView. The CordovaWebView is of course using a webView and therefore I don't understand why the data stored in localStorage in my phoneGap app is not visible in a simple web browser that loads the html page from the same location as the main app. I already researched that the data stored in the CordovaWebView is stored in the file / app_webview / Local Storage / file__0.localstorage , and the data stored in the created webView instance is stored in the file / app_webview / Local Storage / __ 0.localstorage , so I can't access data. I am loading script storage data in localStorage in webView like this:
webView.loadUrl("file:///android_asset/www/script.html");
The master page in the CordovaWebView is loaded in the same way:
void loadUrlNow(String url) {
if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) {
LOG.d(TAG, ">>> loadUrlNow()");
}
if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)) {
super.loadUrl(url);
}
}
where url
file:///android_asset/www/index.html
So why in CordovaWebView localStorage is stored in file
/app_webview/Local Storage/file__0.localstorage
And in webview in file
/app_webview/Local Storage/__0.localstorage
Unfortunately, the strange behavior of localStorage on Android is intentional. You can fix the problem on the native side using a library like this (haven't tried it myself):
- https://github.com/didimoo/AndroidLocalStorage
Or you can use JavaScript to populate localStorage using the executeScript parameter when you open the InAppBrowser and read localStorage when you close it.
- http://docs.phonegap.com/en/edge/cordova_inappbrowser_inappbrowser.md.html#executeScript
This works well for small amounts of data. It can be quite frustrating to debug large amounts.