WebView中使用本地存储拒绝访问

WebView中使用本地存储拒绝访问

本文介绍了在Android WebView中使用本地存储拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android webview,我相信它拥有访问和使用localStorage所需的一切,但是,当我尝试使用本地存储时,我在控制台中看到拒绝访问错误。 Uncaught SecurityError:无法从'Window'读取'localStorage'属性:此文档拒绝访问。

I've got an Android webview, that I believe has everything required to access and use localStorage, however, I'm seeing an "Access denied" error in the console when trying to use local storage. Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.

有人能发现问题吗?

JavaScript代码:

JavaScript Code:

function localStorageTest() {
  // Check browser support
  if (typeof(Storage) != "undefined") {
    // Store
    localStorage.setItem("lastname", "Smith");
    // Retrieve
    document.getElementById("console").innerHTML = localStorage.getItem("lastname");
  } else {
    document.getElementById("console").innerHTML = "Sorry, your browser does not support Web Storage...";
  }
}

这是Android代码:

Here's the Android code:

    // Enable javascript
    WebSettings settings = getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);

    // Create database folder
    String databasePath = getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
    getSettings().setDatabasePath(databasePath);

    getContext().getPackageName() + "/databases/");

    settings.setAppCacheEnabled(true);
    settings.setSaveFormData(true);;
    settings.setLoadWithOverviewMode(true);
    settings.setSaveFormData(true);


推荐答案

访问 localStorage 仅允许来自某些网络安全方案的网页,例如 http: https:文件:。它不适用于关于:数据:方案,或者您可能正在使用的任何自定义方案。 Chrome的行为方式相同,您可以在桌面上查看。

Access to localStorage is only allowed on pages from certain "Web-safe" schemes, like http:, https: and file:. It doesn't work for about: and data: schemes for example, or for any custom scheme you might be using. Chrome behaves the same way, you can check on desktop.

这篇关于在Android WebView中使用本地存储拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:17