Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 on iPhone Safari - data stored by localStorage does not always persist. Why?

I write a simple iPhone web app using HTML5's localStorage.
Tests on a 2G device show that data stored using localStorage does not persist after the Safari process is killed although the opened Safari windows are remembered.

The data is also lost in a case where I am on a different site on a different Safari window, then I change the window to where the web app in subject is shown. When Safari loads the page it automatically refreshes the page. Then the data is lost. This is a simple test code:

<html>
<head>
    <meta name="viewport" content="height=device-height, width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
<script>
    alert("1:" + localStorage.getItem("test"));
    localStorage.setItem("test", "123");
    alert("2:" + localStorage.getItem("test"));
</script>
</body>

As far as I understand the data should persist!
Can anyone shed some light on this behavior? What should I do to get the persistence to work?

Thanks!
Tom.

like image 772
Aerodyne Avatar asked Apr 14 '10 20:04

Aerodyne


People also ask

Does localStorage work on iPhone?

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 on Safari?

In Safari, you can find Local Storage by first going to the Develop menu, then choosing Show Web Inspector, selecting the Storage tab and then selecting Local Storage from the side menu.

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.

Does local storage stay forever?

localStorage works similarly to sessionStorage , but the difference is that data stored in localStorage is persistent and lasts forever for that specific domain unless the browser's cache is cleared or we clear localStorage using JavaScript because localStorage data can only be manipulated by JavaScript.


2 Answers

According to Apple (https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html#//apple_ref/doc/uid/TP40007256-CH6-SW3), localStorage support was added in Safari 4.0.

According to Wikipedia (https://en.wikipedia.org/wiki/Safari_version_history), Safari 4.0 was added in iPhone OS 3.0.

2G iPhones did not come with OS 3.0 or later pre-installed. Your iPhone OS version is most likely too old.

like image 119
user347383 Avatar answered Sep 16 '22 20:09

user347383


I think there is a bug with local storage in iPhone Safari. I have a similar problem as you: http://groups.google.com/group/phonegap/browse_thread/thread/746868e928aaba5c

like image 33
Björn Avatar answered Sep 18 '22 20:09

Björn