ios 停止重新加载从 iPhone 主屏幕启动的网络应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6930771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop reloading of web app launched from iPhone Home Screen
提问by Rand
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.
我创建了一个网络应用程序并添加到我的 iPhone 主屏幕。当我切换到另一个应用程序并返回时,iPhone 会自动重新加载我的网络应用程序。这打破了我的应用程序流程。
How do I prevent iPhone from reloading the app?
如何防止 iPhone 重新加载应用程序?
I have apple-mobile-web-app-capable meta tag enabled to hide Safari toolbar and I don't want to turn it off.
我启用了支持 apple-mobile-web-app 的元标记来隐藏 Safari 工具栏,但我不想将其关闭。
回答by René
I just found this related question on SO: Stop native web app from reloading itself upon opening on iOS
我刚刚在 SO 上发现了这个相关的问题:在 iOS 上打开时停止本地 Web 应用程序重新加载自身
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.
由于它似乎是 Safari 的限制,因此建议的解决方案是使用 Javascript 和 HTML5 localStorage 保持您的网络应用程序状态。当您的 Web 应用程序启动时,检查持久状态并加载它(如果可用)。
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
您可以在此处阅读有关在 Safari 中使用 localStorage 的信息: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. :-)
希望能帮到你。至少对我来说是这样,因为我和你有同样的问题。:-)
回答by Artem Bernatskyi
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 updating
我发现了一个 hack,在iOS 11.4.1/12.0
打开文件上传窗口上进行了测试,然后切换回主屏幕。
该应用程序仍在继续工作,就我而言,音频正在播放并localStorage
正在更新
Proofs: https://youtu.be/heehLUhGKYY
证明:https: //youtu.be/heehLUhGKYY
PS. note how song progress changes when we seek, it proves that app works in the background
附注。注意当我们搜索时歌曲进度如何变化,它证明应用程序在后台运行
回答by Wilbo Baggins
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.
您的问题可能不是实际的重新加载,而是当您的 Web 应用程序通过浏览器打开时,Mobile Safari 对用户的缓存和 cookie 的处理方式与将其作为 Web 应用程序“安装”到主屏幕时不同。尽管这里提出的使用 localStorage 的解决方案会起作用,但如果您的服务器已经负责保持用户的会话状态,则可以避免客户端逻辑的大量工作。30 秒的解决方案是简单地显式设置会话 cookie 以使其具有更长的生命周期。
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.
这使您即使在设备重新启动之间也能保持状态不变,因此即使从主屏幕启动时它在技术上不会阻止重新加载 Web 应用程序,但这是一种在没有他的情况下为用户恢复状态的简单方法/她注意到重新加载 - 在很多情况下我怀疑这是真正的问题。
For a more elaborate discussion of this strategy and code examples, take a look at these questions and my answers there:
有关此策略和代码示例的更详细讨论,请查看这些问题和我在那里的回答:
回答by Alastair
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.
简短的回答是你无法控制这一点。有时,iOS 会在后台保持 Web 应用程序处于活动状态,有时它会杀死它。这完全与设备上可用的内存量有关。
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 using history.pushState()
. This will allow you to reload whatever view the user was on before they switched apps. There are pagehide
and pageshow
eventsthat 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.
因此,您最好的方法是尽量减少此重新加载带来的问题。通过更改location.hash
或使用history.pushState()
. 这将允许您重新加载用户在切换应用程序之前所在的任何视图。有pagehide
和pageshow
活动,让你当用户移动从您的应用程执行代码-利用这个机会来存储的localStorage和/或IndexedDB的本地状态,那么当Web应用被重新打开再次读取这些数据。