Html Iphone localStorage "QUOTA_EXCEEDED_ERR" 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9077101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:10:46  来源:igfitidea点击:

Iphone localStorage "QUOTA_EXCEEDED_ERR" issue

iphoneioshtmllocal-storage

提问by Viren

I trying to use Client Side Storage available in HTML5 (localStorage) for Iphone Application , and I'm completely aware of the "QUOTA" associated for localStorage(which is currently 5MB).

我尝试将 HTML5 (localStorage) 中可用的客户端存储用于 Iphone 应用程序,并且我完全知道与 localStorage 关联的“配额”(当前为5MB)。

Now the problem is for a my application (with no data previously stored) . trying to store the data in localStorage is resulting in QUOTA_EXCEEDED_ERRalthough the size of the overall data is way less than 5 MB (~ 4Kb to be precise ( found using chrome web inspector in normal browser) )

现在问题是我的应用程序(以前没有存储数据)。尝试将数据存储在 localStorage 中会导致QUOTA_EXCEEDED_ERR虽然整体数据的大小小于 5 MB(准确地说是 ~ 4Kb(在普通浏览器中使用 chrome web 检查器找到))

Can anyone Share some light on this that how a data weigh 4Kb is resulting in QUOTA_EXCEEDED_ERRwhen the upper limit for the same 5MB

任何人都可以分享一些关于 4Kb 的数据如何导致QUOTA_EXCEEDED_ERR当上限为相同的5MB

Note that the issue is only occurring for iPhone,all the browsersand even the iPhone Simulatordoesn't prompt with QUOTA_EXCEEDED_ERR error

请注意,此问题仅发生在 iPhone 上所有浏览器甚至iPhone 模拟器都不会提示 QUOTA_EXCEEDED_ERR 错误

iPhone currently is picture is iPhone 4 .

iPhone 目前的图片是 iPhone 4。

回答by JoeCortopassi

Go into Settings->Safari and check to see if private browsing is on. If it is, local storage will not be able to store anything. Here's some basic code to check local storage for you:

进入设置->Safari 并检查隐私浏览是否开启。如果是这样,本地存储将无法存储任何内容。下面是一些基本的代码来为你检查本地存储:

if (!!window.localStorage) 
{
    localStorage.setItem(key, val);
};

Also, how are you setting it? Are you using localStorage.setItem(key, val), or trying localStorage(key, val)? You're problem might be coming from setting it incorrectly

还有,你是怎么设置的?你在使用localStorage.setItem(key, val),还是在尝试localStorage(key, val)?你的问题可能是因为设置不正确

回答by wosis

I had the same issue and JoeCortopassi is only partly right: it is caused by private browsing being enabled. The code provided in that answer does not help much though. When I tested on iPad Safari(ios5) I got

我有同样的问题,JoeCortopassi 只是部分正确:它是由启用隐私浏览引起的。不过,该答案中提供的代码并没有多大帮助。当我在 iPad Safari(ios5) 上测试时,我得到了

console.log(!!window.localStorage); // true

As soon as I try to set a value, I get an exception:

一旦我尝试设置一个值,我就会收到一个异常:

localStorage.setItem("test", "test") // Exception 22 is thrown

So to accurately test for local storage support, it is necessary to try and set a value in local storage, for example:

所以为了准确测试本地存储支持,有必要尝试在本地存储中设置一个值,例如:

var localStorageSupported = function() {
  try {
    localStorage.setItem("test", "test");
    localStorage.removeItem("test");
    return true;
  } catch(e){
    return false;
  }
}

回答by lib3d

The fact is that using private browsing mode on Safari for iOS < 6 does not empty window.localStorageand window.sessionStorageand therefore checking !!window.localStorageor !!window.sessionStoragewon't be enough, and whatever you call from those components will just fail, throwing this QUOTA_EXCEEDED_ERRerror.

事实是,在 iOS < 6 的 Safari 上使用隐私浏览模式不会为空window.localStoragewindow.sessionStorage因此检查!!window.localStorage!!window.sessionStorage将不够,并且您从这些组件调用的任何内容都将失败,引发QUOTA_EXCEEDED_ERR错误。

On those platforms, the private mode seems to set the quota to zero. That's the reason why, to really test those features, the same way Modernizr does, you'll have to wrap it inside a try...catch statement.

在这些平台上,私有模式似乎将配额设置为零。这就是为什么要真正测试这些功能,就像 Modernizr 所做的那样,您必须将它包装在 try...catch 语句中。

Modernizr code :

现代化代码:

var mod = 'modernizr';
/*...*/
tests['localstorage'] = function() {
    try {
        localStorage.setItem(mod, mod);
        localStorage.removeItem(mod);
        return true;
    } catch(e) {
        return false;
    }
};

We have to trust web APIs, but quite carefully.

我们必须信任 Web API,但要非常小心。

回答by Laurent Etiemble

Try deleting the value before setting a new one:

在设置新值之前尝试删除该值:

localStorage.removeItem(key);
localStorage.setItem(key, val);

See also this question, as it looks similar.

另请参阅此问题,因为它看起来相似。

回答by Teemu Ikonen

Local storage quota is tied to domain, if you have other pages that use the data storage they may fill it up. Iterate the keys to find out whats there in the storage when you get the exception.

本地存储配额与域相关联,如果您有其他页面使用数据存储,他们可能会填满它。当您遇到异常时,迭代密钥以找出存储中的内容。

try {
   // try to insert storage here
} catch ( err ) {
   for ( var i =0; i < storage.length ; i++ ) {
       console.log ( storage.key( i ) )
    }
}