HTML5 localStorage无法在Android WebView上运行 [英] HTML5 localStorage not working on Android WebView

查看:897
本文介绍了HTML5 localStorage无法在Android WebView上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Buzztouch为客户制作移动应用程序,因此html页面不在应用程序中,它们是通过JSON从服务器加载的。



客户希望在应用程序上有一个屏幕,用户可以在时间表中输入详细信息并将其存储在手机上。



我是尝试使用localStorage执行此操作,并设置了所有权限等,但在尝试读取或写入页面上的localstorage时出现以下错误:



SecurityError:无法从'Window'读取'localStorage;属性:此文档拒绝访问。



这是在运行Android版本5的Asus Memopad上。 / p>

以下是清单文件中的权限:

 < uses -permission android:name =android.permission.INTERNET/> 
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.READ_PHONE_STATE/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
< uses-permission android:name =android.permission.CAMERA/>
< uses-permission android:name =android.permission.GET_ACCOUNTS/>
< uses-permission android:name =android.permission.READ_CONTACTS/>
< uses-permission android:name =android.permission.WRITE_CONTACTS/>

以下是webView设置:

  webView.getSettings()setDatabaseEnabled(真)。 
webView.getSettings()。setDatabasePath(/ data / data /+ webView.getContext()。getPackageName()+/ databases /);
webView.getSettings()。setDomStorageEnabled(true);

我在较旧的Android平板电脑上运行了完全相同的代码,运行Android版本2.3,我没有得到这个错误,localStorage工作正常。



我也试过iPhone,它工作正常。



我甚至尝试将代码复制到标准网页上,它在平板电脑上的浏览器中运行良好,但webView仍然不喜欢它。



我在网上四处看了几个小时,到目前为止我还没有找到解决办法。



有什么想法吗?



更新:



以下是一些jQuery代码,用于检查localstorage是否可用,以及如果已设置,则加载项目。

 尝试{
if(typeof(Storage)!==undefined ){
try {
if(localStorage.getItem(storage)!== null){
storage = JSON.parse(localStorage.getItem(storage));
}
} catch(e){
$(#error)。html($(#error)。html()++ e +< br> ;);
$(#error)。css(display,block);
}
} else {
$(#log)。html(HTML5存储无效。);
}
} catch(e){
$(#error)。html($(#error)。html()++ e +< br> ;);
$(#error)。css(display,block);
}

这是在加载时引发错误的原因,因为这是在$中(文件).ready section。

解决方案

我终于设法解决了这个问题。



无论我尝试什么,我能让它工作的唯一方法是在应用程序中包含html文件,而不是通过Buzztouch的控制面板进行,并加载它。



此html文件现在正确加载且没有localStorage错误。


I'm in the process of making a mobile app for a client, using Buzztouch, so the html pages are not within the app, they are loaded in from the server via JSON.

The client would like to have a screen on the app where users can enter details into a timetable and store it on the phone.

I'm trying to do this using localStorage, and have setup all the permissions, etc but I get the following error when trying to read or write to the localstorage on the page:

"SecurityError: Failed to read the 'localStorage; property from 'Window': Access is denied for this document."

This is on an Asus Memopad running Android version 5.

Here are the permissions in the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

Here are the webView settings:

webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
webView.getSettings().setDomStorageEnabled(true);

I've tried the exact same code on an older Android tablet, running Android version 2.3, and I don't get this error, localStorage works fine.

I've also tried in on an iPhone and it works fine.

I even tried copying the code onto a standard webpage and it works fine in the browser on the tablet, but webView still doesn't like it.

I've looked around on the net for hours, and so far I haven't found a solution to this.

Any ideas?

UPDATE:

Here is some of the jQuery code, it is meant to check if localstorage is available, and load in an item if it is set.

try {
    if(typeof(Storage) !== "undefined") {
        try {
            if(localStorage.getItem("storage") !== null) {
                storage = JSON.parse(localStorage.getItem("storage"));
            }
        } catch(e) {
            $("#error").html($("#error").html() + " " + e + "<br>");
            $("#error").css("display", "block");
        }
    } else {
        $("#log").html("HTML5 Storage is not working.");
    }
} catch(e) {
    $("#error").html($("#error").html() + " " + e + "<br>");
    $("#error").css("display", "block");
}

This is what is throwing the error on load, as this is in the $(document).ready section.

解决方案

I've finally managed to solve this problem.

The only way I could get it to work, no matter what I tried, was to include the html file within the app, rather than doing it through Buzztouch's control panel, and loading that in.

This html file now loads correctly and with no localStorage errors.

这篇关于HTML5 localStorage无法在Android WebView上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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