IOS下的safari下localStorage不起做用的問題

咱們的一個小應用,使用百度地圖API獲取到用戶的座標以後用localStorage作了下緩存,測試上線以後有運營同窗反饋頁面數據拉取不到,javascript

測試的時候沒有發現問題,並且2臺相同的iphone一臺能夠一臺不行,最後定位到是localStorage有問題。java

可是爲啥明明版本同樣的2臺手機一臺能夠一臺不行呢?web

並且2臺手機打印localStorage明明都是Object,奇怪了。緩存

在度娘上沒找到答案,最後到google發現有人說是Private Browsing Mode引發的。而後查看IOS的safari沒有發現隱私設置什麼的iphone

後來點擊右下角小框框發現有個無痕瀏覽的模式。MD,關閉後一切正常測試

// 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.');
    }
}

 能夠醬紫判斷。提示用戶關閉無痕模式。this

相關文章
相關標籤/搜索