简体   繁体   English

Android 4.0 - > 4.3(包含) - Webview页面之间的Web存储丢失

[英]Android 4.0 -> 4.3 (included) - Web storage lost between webview pages

I'm working on an Android project which relies on the WebView to browse multiple HTML pages stored on the device and submit the inputs toward the webview when it's needed for storing them in a database. 我正在开发一个Android项目,该项目依赖于WebView来浏览存储在设备上的多个HTML页面,并在需要将输入存储到数据库中时将其提交到webview。

Each page contains controls that are bound with jQuery towards previous / next pages, each page contains inputs of differents types (checkboxes, textfields, etc.). 每个页面都包含与jQuery绑定到上一页/下一页的控件,每个页面包含不同类型的输入(复选框,文本字段等)。

The last page contains a Submit buttons that uses the JSInterface to save the results inside a SQLite DB. 最后一页包含一个Submit按钮,它使用JSInterface将结果保存在SQLite DB中。

Another button (in a custom top navigation bar) offers the same system. 另一个按钮(在自定义顶部导航栏中)提供相同的系统。

Results can be modified by accessing the first page with all the saved inputs, a jQuery system will fill the corresponding inputs. 可以通过访问包含所有已保存输入的第一页来修改结果,jQuery系统将填充相应的输入。

For more details, I'm using the SDK 19 and compiling against 4.4.2 , but I used to work with the SDK 15 and compiling against 4.2.2 where I didn't have the issue. 有关更多详细信息,我正在使用SDK 19并针对4.4.2进行编译,但我曾经使用SDK 15并针对4.2.2进行编译,我没有遇到此问题。

If someone needs to see what is done, in a simplified system, check this JSBin . 如果有人需要查看已完成的操作,请在简化的系统中查看此JSBin


The issue 问题

I'm using SessionStorage to store the inputs between the pages, I used to work with cookies but they became unreliable when there was over 150 key/values pairs. 我正在使用SessionStorage存储页面之间的输入,我曾经使用过cookie但是当有超过150个键/值对时它们变得不可靠。

My problem is that on some devices, the SessionStorage disappear between pages. 我的问题是在某些设备上, SessionStorage在页面之间消失。


Test protocol 测试协议

1st case - Staying on the first page only 第一种情况 - 只留在第一页

If I stay on the first page only, fill the inputs then send the results, everything's fine. 如果我只留在第一页,填写输入然后发送结果,一切都很好。 Coming back for modification offers me a fully filled first page. 回来修改为我提供了一个完整的第一页。

2nd case - Moving between pages 第二种情况 - 在页面之间移动

After filling page 1, I move to page 2 and fill the new inputs then move between pages to see if the inputs are lost on each individual pages. 填写第1页后,我转到第2页并填充新输入,然后在页面之间移动,以查看每个页面上的输入是否丢失。 Everything is in place, but if I send the results, only the current page inputs are transmitted. 一切都到位,但如果我发送结果,则只传输当前页面输入。


Android versions test results Android版测试结果

  • 3.2 - Works 3.2 - 工程

  • 4.1.2 - Doesn't work 4.1.2 - 不起作用

  • 4.2.1 - Doesn't work 4.2.1 - 不起作用

  • 4.3 - Doesn't work 4.3 - 不起作用

  • 4.4.2 - Works 4.4.2 - 工程


Tested solutions 测试解决方案

  • Overriding the WebViewClient 's shouldOverrideUrlLoading method to return False - Doesn't work 覆盖WebViewClientshouldOverrideUrlLoading方法以返回False - 不起作用

  • Using LocalStorage instead of SessionStorage didn't change a thing 使用LocalStorage而不是SessionStorage并没有改变一件事

Insights 洞察

Switching from sessionStorage to localStorage did not help. sessionStorage切换到localStorage没有帮助。

I found some useful informations about the WebKit versions used by Android : 我发现了一些有关Android使用的WebKit版本的有用信息

Android 3.2.1 uses a quite old version but it works (v534.13) Android 3.2.1使用了相当旧的版本,但它有效(v534.13)

Android versions ranging from 4.0 to 4.3 share the same WebKit engine (v534.30) 4.04.3的 Android版本共享相同的WebKit引擎(v534.30)

Android 4.4 uses a brand new version (v537.36) of it, which explains why it works Android 4.4使用了它的全新版本(v537.36),这解释了它的工作原理

Not a single step toward a fix BUT it gives a more precise view of the problem and the device it affects. 没有一个步骤可以解决问题,但它可以更准确地查看问题及其影响的设备。

Solution

Since the SDK 16, a new security setting has been forced to prevent Javascript code to access content from any origin. 自SDK 16以来,新的安全设置被迫阻止Javascript代码访问来自任何来源的内容。

if(Build.VERSION.SDK_INT >= 16) {
    setting.setAllowUniversalAccessFromFileURLs(true);
}

Kudos to ksasq for finding this ! 感谢ksasq找到这个!

[EDIT 18/02/2014] [编辑18/02/2014]

After some testing, I pin pointed the problem to the TargetSdkVersion, the BuildTarget does not change anything. 经过一些测试,我将问题指向TargetSdkVersion,BuildTarget不会改变任何东西。

It it's set to 15 , the WebStorage works as intended. 它设置为15 ,WebStorage按预期工作。

If it's set to 16 or higher, the WebStorage is messing up. 如果它设置为16或更高,WebStorage就会搞乱。

Between ICS (SDK 15) and JellyBean (SDK 16) there were some changes in the WebView's security model and how it handles javascript from file:// origins. 在ICS(SDK 15)和JellyBean(SDK 16)之间,WebView的安全模型以及它如何处理来自file:// origin的javascript有一些变化。 Please try to call 请尝试打电话

WebSettings.setAllowUniversalAccessFromFileURLs(true)

to acknowledge you are working from file:// URLs and trust the content you are displaying. 确认您正在使用file:// URL并信任您正在显示的内容。 I imagine that due to the upgrade to Chromium WebView in 4.4 something else changed in the underlying implementation to not require these settings. 我想,由于在4.4中升级到Chromium WebView,底层实现中的其他更改不需要这些设置。

I'm sorry to hear that webstorage api has an issue on Android, because I will be using this in next few days. 我很遗憾听到webstorage api在Android上存在问题,因为我将在未来几天内使用它。

As you have investigated already, Android 4.4 uses a brandnew WebView which is based on the ChromeBrowser where lot of web technology such as webScoket etc other than localStorage api are already supported well. 正如您已经调查过的那样,Android 4.4使用了一个基于ChromeBrowser的全新WebView,其中很多网络技术如webScoket等除了localStorage api已经得到了很好的支持。

Having said that, reading your post claims localStorage api has an issue for android4.0-4.2 (which I also aim as target devices), I thought using FileSystem api instead. 话虽如此,阅读你的帖子声称localStorage api有一个问题android4.0-4.2(我也瞄准目标设备),我想使用FileSystem api代替。

http://www.html5rocks.com/en/tutorials/file/filesystem/ http://www.html5rocks.com/en/tutorials/file/filesystem/

However, looking at web, it is so uncertain if this api works well, and finally I found this. 然而,看着网络,这个api是否运作良好是如此不确定,最后我发现了这一点。

Using local storage on Android webview 在Android webview上使用本地存储

https://github.com/didimoo/AndroidLocalStorage https://github.com/didimoo/AndroidLocalStorage

My understanding is that session storage exists while the window that created it still open and local storage is persistent between sessions. 我的理解是会话存储存在,而创建它的窗口仍然打开,本地存储在会话之间是持久的。 So does the browser treats the window to remain open while navigating between two local files? 那么浏览器会在两个本地文件之间导航时将窗口保持打开状态吗? This could be open to interpretation. 这可以解释。 Looking at your code it seems fairly easy to change it to use local storage rather than session storage, so would that be an option? 查看代码,将其更改为使用本地存储而不是会话存储似乎相当容易,那么这是一个选项吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM