问题
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).
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_ERR although the size of the overall data is way less than 5 MB (~ 4Kb to be precise ( found using chrome web inspector in normal browser) )
Can anyone Share some light on this that how a data weigh 4Kb is resulting in QUOTA_EXCEEDED_ERR when the upper limit for the same 5MB
Note that the issue is only occurring for iPhone ,all the browsers and even the iPhone Simulator doesn't prompt with QUOTA_EXCEEDED_ERR error
iPhone currently is picture is iPhone 4 .
回答1:
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:
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
回答2:
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
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;
}
}
回答3:
The fact is that using private browsing mode on Safari for iOS < 6 does not empty window.localStorage
and window.sessionStorage
and therefore checking !!window.localStorage
or !!window.sessionStorage
won't be enough, and whatever you call from those components will just fail, throwing this QUOTA_EXCEEDED_ERR error.
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 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.
回答4:
Try deleting the value before setting a new one:
localStorage.removeItem(key);
localStorage.setItem(key, val);
See also this question, as it looks similar.
回答5:
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 ) )
}
}
来源:https://stackoverflow.com/questions/9077101/iphone-localstorage-quota-exceeded-err-issue