share localStorage across webView and CordovaWebVi

2019-06-27 04:29发布

I am trying to share data in activity between webView and CordovaWebView. CordovaWebView of course using webView and that is why I do not understand why data stored in localStorage in my phoneGap app is not visible in simple webView which load html page from the same location as main application does. I've already investigated that data stored by the CordovaWebView is stored in file /app_webview/Local Storage/file__0.localstorage and data stored by created instance of webView class is stored in file /app_webview/Local Storage/__0.localstorage that is why I cannot access data. I am loading the script storing data in localStorage to webView like this:

webView.loadUrl("file:///android_asset/www/script.html");

Main page in 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 is

file:///android_asset/www/index.html

So why in CordovaWebView localStorage is saved in file

/app_webview/Local Storage/file__0.localstorage

And in webview in file

/app_webview/Local Storage/__0.localstorage

1条回答
2楼-- · 2019-06-27 04:41

Unfortunately the weird behavior of localStorage on Android is intentional. You could either fix the problem on the native side by using a library like this one (Didn't try this myself):

Or you can use JavaScript to populate localStorage using the executeScript parameter when opening the InAppBrowser and read localStorage when closing it.

This works well for small amounts of data. Can be quite annoying to debug for large amounts though.

查看更多
登录 后发表回答