Stop native web app from reloading itself upon opening on iOS

一个人想着一个人 提交于 2019-12-17 23:44:12

问题


I'm trying to build a "native web app" using HTML + JS on iOS. As you may know you can add such an application to the homescreen and it will more or less look just like a normal native app.

However if I quit such an app and reopen it again it reloads the whole page again. This also happens when switching to such an application from another over the multitasking bar.

Is this expected behaviour or is there a way to stop the device from doing this?

As an example you can add the jqTouch-Demos from here to your homescreen and test it: http://jqtouch.com/preview/demos/main/


回答1:


You could save the state of your app in localStorage. On restart, check to see if the state is running, then restore the app to where it last was.




回答2:


Same problem here. Anyway, if you don't want to reinvent the wheel you can use a tool like PhoneGap (http://www.phonegap.com/). Native web application wrapper with built in access to a number of native features. Also, you store the application locally (fast, secure) and you can of course charge for it ;) It's under BSD or MIT license.




回答3:


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?




回答4:


You might want to look at using cache-manifest to prevent it loading the files.

Matt Might has a good writeup here:

http://matt.might.net/articles/how-to-native-iphone-ipad-apps-in-javascript/

Basically you change the html tag to this

<html manifest="cache.manifest">

and write a cache.manifest file on the server which specifies which files should be kept in the device cache and which should be reloaded dynamically from the network.

CACHE MANIFEST
local1.file
local2.file

NETWORK:
network1.php
network2.cgi

You also need to make sure your web server serves up .manifest files with the MIME type text/manifest, or else this won't work. If you're using apache, put the following in your .htaccess file:

AddType text/cache-manifest .manifest


来源:https://stackoverflow.com/questions/4291784/stop-native-web-app-from-reloading-itself-upon-opening-on-ios

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