Iphone localStorage “QUOTA_EXCEEDED_ERR” issue

前端 未结 5 1943
孤街浪徒
孤街浪徒 2020-12-30 02:19

I trying to use Client Side Storage available in HTML5 (localStorage) for Iphone Application , and I\'m completely aware of the \"QUOTA\" associated for loc

相关标签:
5条回答
  • 2020-12-30 02:37

    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 ) )
        }
    }
    
    0 讨论(0)
  • 2020-12-30 02:43

    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.

    0 讨论(0)
  • 2020-12-30 02:53

    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

    0 讨论(0)
  • 2020-12-30 02:57

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-30 03:00

    Try deleting the value before setting a new one:

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

    See also this question, as it looks similar.

    0 讨论(0)
提交回复
热议问题