Tag Archives: localStorage

The problem and solution of local storage in Safari under IOS

One of our small applications uses Baidu map API to get the user’s coordinates, and then uses localstorage to do the next cache. After the test goes online, some students from the operation feedback that the page data can’t be pulled

No problem was found during the test, and one of the two identical iPhones could work and the other could not. Finally, it was found that there was a problem with localstorage

But why is it that two mobile phones of the same version can be used one by one

And the two mobile phones are all objects. It’s strange

I didn’t find the answer on Du Niang. Finally, I went to Google and found that it was caused by private browsing mode. Then check the safari of IOS and find no privacy settings

Later, click on the small box in the lower right corner to find a traceless browsing mode. The solution is as follows:

Method 1

// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
// throw QuotaExceededError. We’re going to detect this and just silently drop any calls to setItem
// to avoid the entire page breaking, without having to do a check at each usage of Storage.
if (typeof localStorage === ‘object’) {
try {
localStorage.setItem(‘localStorage’, 1);
localStorage.removeItem(‘localStorage’);
} catch (e) {
Storage.prototype._ setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function() {};
alert(‘Your web browser does not support storing settings locally. In Safari, the most common cause of this is using “Private Browsing Mode”. Some settings may not save or some features may not work properly for you.’);
}
}

Method 2

Using cookies to store data (for small amount of data)

Traceless mode should disable the localstorage object, but window. Localstorage exists, but document. Cookie can be used

 try {
        localStorage.setItem('isPrivateMode', '1');
        localStorage.removeItem('isPrivateMode');
        window.isPrivateMode = false;
    } catch(e) {
        window.isPrivateMode = true;
    }
    if (!window.isPrivateMode && window.localStorage) { // 不是 Safari 无痕模式并且能用 localStorage
        localStorage.removeItem('scheme');
        localStorage.setItem('scheme',scheme);

        localStorage.removeItem('caiqrhost');
        localStorage.setItem('caiqrhost',caiqrhost);

        localStorage.removeItem('paytype');
        localStorage.setItem('paytype',paytype);

        localStorage.removeItem('classname');
        localStorage.setItem('classname',classname);
    }
    else{
        try{
            var cookie = new cookies();
            cookie.delCookie('scheme');
            cookie.delCookie('caiqrhost');
            cookie.delCookie('paytype');
            cookie.delCookie('classname');

            cookie.setCookie('scheme',scheme,30);
            cookie.setCookie('caiqrhost',caiqrhost,30);
            cookie.setCookie('paytype',paytype,30);
            cookie.setCookie('classname',classname,30);
            //alert(cookie.getCookie('classname'));
        }
        catch(e){
            alert(e);
        }

    }

cookie.operate.js

var cookies = function(){};
cookies.prototype = {
    setCookie:function(c_name, value, expiredays){
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ?"" : ";expires="+exdate.toGMTString());
    },
    getCookie:function(name){
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

        if(arr=document.cookie.match(reg)){
            return (arr[2]);
        }
        else{
            return null;
        }

    },
    delCookie:function(name){
        var cookie = new cookies();
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval=cookie.getCookie(name);
        if(cval!=null){
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }        
    }
}

http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an