Android 4.0.1 打破了 WebView HTML 5 本地存储? [英] Android 4.0.1 breaks WebView HTML 5 local storage?

查看:15
本文介绍了Android 4.0.1 打破了 WebView HTML 5 本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 html5 测试页面,它使用 LocalStorage 来显示/保存/重新显示一段数据.

I have a simple html5 test page which uses LocalStorage to display / save / redisplay a piece of data.

此代码在 Android 2.3.x 中完美运行,但 记录 4.0.1 中 html 第 18 行的异常,这是第一个 localStorage.getItem() 调用此时 JS 停止.

This code works perfectly in Android 2.3.x but logs an exception in 4.0.1 on line 18 of the html which is the frist localStorage.getItem() call and at this point the JS stops.

异常:未捕获的错误:SECURITY_ERR:DOM Exception 18 at/data/data/my.app.name/app_htmlData:18我还尝试将数据库路径设置为 getCacheDir(),结果相同.

Exception: Uncaught Error: SECURITY_ERR: DOM Exception 18 at /data/data/my.app.name/app_htmlData:18 I've also tried setting the database path to getCacheDir() with the same result.

String htmlContent = "HTML content listed below";    
File sharedDir = getActivity().getDir("htmlData", Context.MODE_PRIVATE);
WebView browser = (WebView)v.findViewById(R.id.wvBrowser);

browser.setWebChromeClient(new WebChromeClient(){
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long  currentQuota, long estimatedSize,   long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(estimatedSize * 2); 
        }
    });       
browser.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url){

        view.loadUrl("javascript:doTest()");

    });

browser.getSettings().setDatabaseEnabled(true);
browser.getSettings().setDatabasePath(sharedDir.getPath());
browser.getSettings().setDomStorageEnabled(true);
browser.loadDataWithBaseURL(mSharedDir.getPath(), 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);

页面渲染的HTML如下:

The HTML that the page is rendering is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple localStorage test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

        function doTest() {
            $('#stuff').append('<p>reading</p>');
            var item = read();

            $('#stuff').append('<p>writing</p>');
            localStorage['bar'] = new Date().toUTCString();

            $('#stuff').append('<p>&nbsp;</p><p>reading again</p>');
            read();
        }
        function read() {
            var item = localStorage.getItem('bar');
            if (item == null || (item == undefined)) {
                item = '';
            }
            $('#stuff').append('<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item: ' + item + '</p>');

         return item;
        }
    </script>
</head>
<body>
    <p>-Simple localStorage test-</p>
    <div id="stuff"></div>
</body>
</html>

可用来源此处

推荐答案

通过与 Google 工程师的一些讨论,他们似乎已经决定 file://方案不安全.

Via some discussion with a Google engineer it seems that they've made the decision that the file:// scheme is insecure.

解决此问题的方法是执行以下操作

A work around for this is to do the following

browser.loadDataWithBaseURL("http://www.example.com", 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);

这篇关于Android 4.0.1 打破了 WebView HTML 5 本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆