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

时间秒杀一切 提交于 2019-12-21 09:14:02

问题


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.


回答1:


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.




回答2:


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




回答3:


I tried your code on my iPhone 3G and Safari on Windows and it works. Maybe you are running an older version of Safari on your iPhone?



来源:https://stackoverflow.com/questions/2640724/html5-on-iphone-safari-data-stored-by-localstorage-does-not-always-persist-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!