共享localStorage跨webView和CordovaWebView android [英] share localStorage across webView and CordovaWebView android
问题描述
我想在webView和CordovaWebView之间共享活动中的数据。 CordovaWebView当然使用webView,这就是为什么我不明白为什么数据存储在localStorage在我的phoneGap应用程序是不可见的简单webView哪个加载html页面与主应用程序相同的位置。我们已经调查过, CordovaWebView 存储的数据存储在 / app_webview / Local Storage / file__0.localstorage 文件中,并且由创建的 webView 类存储在文件 / app_webview / Local Storage / __ 0.localstorage 中,这就是为什么我无法访问数据。
我将在localStorage中存储数据的脚本加载到webView,如下所示:
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");
CordovaWebView中的主页以相同的方式加载:
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);
}
}
其中url是
file:///android_asset/www/index.html
所以为什么在CordovaWebView localStorage保存在文件中
So why in CordovaWebView localStorage is saved in file
/app_webview/Local Storage/file__0.localstorage
并在文件中的webview中
And in webview in file
/app_webview/Local Storage/__0.localstorage
推荐答案
不幸的是,localStorage在Android上的怪异行为是故意的。您可以通过使用这样的库来解决本机问题(没有自己尝试):
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):
- https://github.com/didimoo/AndroidLocalStorage
或者,您可以使用JavaScript在打开InAppBrowser时使用executeScript参数来填充localStorage,并在关闭时读取localStorage。
Or you can use JavaScript to populate localStorage using the executeScript parameter when opening the InAppBrowser and read localStorage when closing it.
- http://docs.phonegap.com/en/edge/cordova_inappbrowser_inappbrowser.md.html#executeScript
这适用于少量数据。可以很烦人调试大量虽然。
This works well for small amounts of data. Can be quite annoying to debug for large amounts though.
这篇关于共享localStorage跨webView和CordovaWebView android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!