我只需要在 iOS 独立网络应用程序中保留一些简单数据(使用 apple-mobile-web-app-capable
等)。数据在用户访问网站时定义,并保存到 localStorage.name
等。同一页面“保存到主屏幕”但是当我到达那里时,输出 localStorage.name
etc 返回 undefined
。
我回到 Safari,它仍然知道这些信息。我使用笔记本电脑的开发人员控制台将其拉出,但我仍然可以获得所有数据。它甚至不是不同的页面,更不用说不同的域或任何东西了。我读到的所有内容都告诉我,localStorage
应该在 Safari 和独立应用程序之间共享(对于修改该数据有一些小警告)。我错过了什么?
这是代码的要点:
if (!window.navigator.standalone) {
// show Safari content
if (localStorage.name != void 0 && localStorage.var2 != void 0) {
// show normal, "add this to your home screen" language -
// this screen shows properly
} else {
// show "We weren't passed our variables" language
}
} else {
// Show standalone/offline content
if (localStorage.name != void 0 && localStorage.var2 != void 0) {
// Show normal standalone app content using localStorage variables.
// I'm not seeing this.
} else {
// Show "error" text, which is what I'm getting
document.body.removeChild(document.getElementById('container'));
var helper = document.createElement('section')
helper.id="helper"
helper.innerHTML = '<h1>Sorry</h1>'
+ '<p>There seems to be a problem with the app.</p>'
+ '<pre>'
+ " Name: " + localStorage.name + "\n"
+ " var2: " + localStorage.var21 + "\n"
+ '</pre>';
document.body.insertBefore(helper,document.body.firstChild)
}
}
无论如何,我使用的是 iOS 6.1。所以这不应该是一个“老”问题。我做错了什么?
最佳答案
这条评论:iOS 'Web App' has different localStorage than Mobile Safari
...有答案。本地存储现在完全独立于 Safari 和 native 应用程序。
关于javascript - LocalStorage 没有传递给独立的网络应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720063/