Stop native web app from reloading itself upon ope

2019-01-21 05:39发布

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/

4条回答
家丑人穷心不美
2楼-- · 2019-01-21 06:18

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.

查看更多
Fickle 薄情
3楼-- · 2019-01-21 06:27

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:

查看更多
不美不萌又怎样
4楼-- · 2019-01-21 06:28

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
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-21 06:36

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.

查看更多
登录 后发表回答