Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop native web app from reloading itself upon opening on iOS

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/

like image 794
cguedel Avatar asked Nov 27 '10 13:11

cguedel


3 Answers

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.

like image 113
ghenne Avatar answered Nov 13 '22 21:11

ghenne


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.

like image 38
Giorgio Luparia Avatar answered Nov 13 '22 21:11

Giorgio Luparia


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?

like image 2
Wilbo Baggins Avatar answered Nov 13 '22 21:11

Wilbo Baggins