Stop reloading of web app launched from iPhone Home Screen

前端 未结 4 2039
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 20:04

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.

H

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-22 20:33

    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 using history.pushState(). This will allow you to reload whatever view the user was on before they switched apps. There are pagehide and pageshow 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.

提交回复
热议问题