停止重新加载从 iPhone 主屏幕启动的 Web 应用程序
我创建了一个 Web 应用程序并将其添加到我的 iPhone 主屏幕。当我切换到另一个应用程序并返回时,iPhone 会自动重新加载我的网络应用程序。这破坏了我的应用程序流程。
如何防止 iPhone 重新加载应用程序?
我启用了支持 apple-mobile-web-app 的元标记来隐藏 Safari 工具栏,但我不想将其关闭。
I created a web app and added to my iPhone Home Screen. When I switch to another app and back, iPhone automatically reload my web app. This breaks my app flow.
How do I prevent iPhone from reloading the app?
I have apple-mobile-web-app-capable meta tag enabled to hide Safari toolbar and I don't want to turn it off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚发现这个相关问题: 停止原生 Web 应用程序在 iOS 上打开时不会重新加载
由于这似乎是 Safari 的限制,因此建议的解决方案是使用 Javascript 和 HTML5 localStorage 保存 Web 应用程序状态。当您的 Web 应用程序启动时,检查持久状态并加载它(如果可用)。
您可以在此处阅读有关在 Safari 中使用 localStorage 的信息:http://developer.apple.com/library/safari/#documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007256-CH1-SW1
希望这对你有帮助。至少对我来说是这样,因为我和你有同样的问题。 :-)
I just found this related question on SO: Stop native web app from reloading itself upon opening on iOS
As it seems it's a limitation of Safari, a proposed solution is to persist your web apps state using Javascript and HTML5 localStorage. When your web app is launched, check for the persisted state and load it if available.
You can read about using localStorage in Safari here: http://developer.apple.com/library/safari/#documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007256-CH1-SW1
Hope that helps you. At least it did for me, as I had the same problem as you. :-)
简而言之,你无法控制这一点。有时,iOS 会让网络应用程序在后台保持活动状态,有时则会杀死它。这完全与设备上有多少可用内存有关。
因此,最好的方法是尽量减少重新加载带来的问题。确保您的 Web 应用程序在您从一个视图移动到另一个视图时更新 URL,方法是更改
location.hash
或使用history.pushState()
。这将允许您重新加载用户在切换应用程序之前所处的任何视图。有pagehide
和pageshow
事件 允许您在用户离开您的应用程序时执行代码 - 借此机会将本地状态存储在 localStorage 和/或 IndexedDB 中,然后在重新打开 Web 应用程序时再次获取该数据。The short answer is that you can't control this. Sometimes iOS will keep a web app active in the background, at other times it will kill it. It's entirely related to how much memory is available on the device.
So, your best approach is to minimise the problems presented by this reload. Make sure your webapp updates the URL when you move from view to view, either by changing
location.hash
or usinghistory.pushState()
. This will allow you to reload whatever view the user was on before they switched apps. There arepagehide
andpageshow
events that allow you to execute code when the user moves away from your app - take that opportunity to store local state in localStorage and/or IndexedDB, then fetch that data again when the webapp is reopened.我发现了一个 hack,在
iOS 11.4.1/12.0
上进行了测试
打开文件上传窗口,然后切换回主屏幕。
该应用程序仍然继续工作,在我的例子中,音频正在播放,并且
localStorage
正在更新校样:
https://youtu.be/heehLUhGKYY
注意当我们寻找时歌曲进度如何变化,这证明应用程序在后台运行
I found a hack, tested on
iOS 11.4.1/12.0
Open file uploading window and then switch back to the home screen.
The app still continues to work, in my case audio is playing and
localStorage
is updatingProofs:
https://youtu.be/heehLUhGKYY
PS. note how song progress changes when we seek, it proves that app works in the background
更新:由于此答案收到反对票,我添加了此解释。
您的问题可能不是实际的重新加载,而是事实上,当您的 Web 应用程序通过浏览器打开时,与作为 Web 应用程序“安装”到主屏幕时相比,Mobile Safari 会以不同的方式处理用户的缓存和 Cookie。尽管这里提出的使用 localStorage 的解决方案可以工作,但是如果您的服务器已经负责保存用户的会话状态,那么它们对于客户端逻辑来说是大量的工作,这是可以避免的。 30 秒的解决方案是简单地显式设置会话 cookie 以使其具有更长的生命周期。
这允许您即使在设备重新启动之间也能保持状态完整,因此即使从技术上来说它并不能阻止 Web 应用程序在从主屏幕启动时重新加载,但这是一种无需用户即可恢复状态的简单方法/她注意到重新加载 - 在很多情况下我怀疑这才是真正的问题。
有关此策略和代码示例的更详细讨论,请查看这些问题和我的答案:
在 iPhone 上的 Web 应用程序中维护 PHP 会话
iPhone“书签到主屏幕”会删除cookie并
Update: as this answer is receiving downvotes, I added this explanation.
Your problem might not be the actual reload, but the fact that Mobile Safari treats your user's cache and cookies differently when your web app is opened through the browser, than when it's 'installed' as a web app to the home screen. Although the solutions proposed here that use localStorage will work, they're a lot of work for client-side logic that can be avoided if your server is already responsible for persisting the session state of your user. The 30-second solution is to simply explicitly set the session cookie to have a longer lifetime.
This allows you to keep the state intact even between device reboots, so even though it doesn't technically stop the web app from being reloaded when launched from the home screen, it is an easy way to restore the state for the user without him/her noticing the reload - which in many cases I suspect is the real problem.
For a more elaborate discussion of this strategy and code examples, take a look at these questions and my answers there:
Maintain PHP Session in web app on iPhone
iPhone "Bookmark to Homescreen" removes cookies and session?