Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Safari Private Browsing localStorage and sessionStorage Support?

I have found a few questions here on StackOverflow addressing specific functionality with iOS Safari Private Browsing and sessionStorage and localStorage. But I haven't been able to find a definitive resource denoting the support that iOS Safari has for sessionStorage and localStorage when Private Browsing.

What support is there for this or is there any specific resource from Apple denoting this functionality? The general consensus is that localStorage is not at all supported without a polyfill, does the same goes for sessionStorage?

Thank you so much!

like image 860
Aaron Brewer Avatar asked Feb 24 '16 16:02

Aaron Brewer


People also ask

Does Safari support localStorage?

Follow me on twitch! In short, you can't, but you can set a cookie via JavaScript 😉 Safari on iOS supports localStorage, but in Private Mode it simply throws an error when you try to save anything to it, which is not great. Also it breaks the behaviour of your app on iPhones and iPads.

Does localStorage work in incognito mode?

Another difference is that, in the case of a few browsers, localStorage doesn't work in incognito mode but sessionStorage does.

Does Safari delete localStorage?

Specifically, Safari will erase IndexedDB, LocalStorage, Media keys, SessionStorage, and Service Worker registrations after seven days if the user does not interact with the associated website during this period.

Should I use localStorage or sessionStorage?

The difference between sessionStorage and localStorage is that localStorage data does not expire, whereas sessionStorage data is cleared when the page session ends. A unique page session gets created once a document is loaded in a browser tab. Page sessions are valid for only one tab at a time.


2 Answers

Yes, same goes for sessionStorage and localStorage.

There is an excellent Gist by Paul Irish explaining the history of the issue:

https://gist.github.com/paulirish/5558557

Best solution if you only need one of them:

function isLocalStorageEnabled() {
  try {
      var mod = '__storage_test__';
      localStorage.setItem(mod, mod);
      localStorage.removeItem(mod);
      return true;
  } catch(e) {
      return false;
  }
}

Or, to make it work for both, the MDN-recommended solution is more-generic: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API

function storageAvailable(type) {
    try {
        var storage = window[type];
        var x = '__storage_test__';
        storage.setItem(x, x);
        storage.removeItem(x);
        return true;
    }
    catch(e) {
        return false;
    }
}
like image 131
theUtherSide Avatar answered Oct 13 '22 18:10

theUtherSide


I don't think there is any specific resource for iOS, but here's Apple's official documentation:

https://developer.apple.com/library/safari/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html

And this StackOverflow question is pretty useful as well:

QuotaExceededError: Dom exception 22: An attempt was made to add something to storage that exceeded the quota

In general, when solving for sessionStorage and localStorage, try actually developing locally with Safari on your phone with Web Inspector open. Good luck :)

like image 22
abhi Avatar answered Oct 13 '22 19:10

abhi