3

I am in the process of developing a Web App for iOS and am using the DOMApplicationCache. I know that Web Apps can not run in the background, but I still would like to simulate a resume of the app when the user switches to another app and returns to the web app later.

In order to do this I at least need to record when the Web App terminates/unloads. But the window.unload does not seem to fire.

Does anybody have an idea how to catch the termination of a WebApp running?

PS: on a related question, where does the console.log go when apple-mobile-web-app-capable is yes?

woz
  • 10,888
  • 3
  • 34
  • 64
Jamgold
  • 1,716
  • 1
  • 14
  • 18

2 Answers2

6

You can use the "pageshow" event to detect when the app has resumed. This event will fire when user returns to your app from a different safari tab. It will also fire when user clicks the home button and then comes back to the app by clicking on safari button

     <script>
    window.addEventListener("pageshow", function(){
        alert("page shown");
    }, false);
  </script>

I have tested this on the iOS simulator and its working as I described above.

user566245
  • 4,011
  • 1
  • 30
  • 36
3

I have an answer. It's not what we were hoping for.

The technical definition of unload from http://www.w3.org/TR/DOM-Level-2-Events/events.html is:

The unload event occurs when the DOM implementation removes a document from a window or frame. This event is valid for BODY and FRAMESET elements.

I also got a reply from some in the know at Apple: "Unfortunately, I haven't been able to find an event that fires when the Home button is clicked! I think the most appropriate event for this might be the pagehide event. This event actually does fire in Safari on iOS when you switch to the "tabs" view to open a new page, but not when you click the home screen icon :-("

ghenne
  • 1,903
  • 2
  • 19
  • 29
  • 2
    thanks for taking the time to respond. So the only way to determine if a webapp has been "resumed" is to compare a heartbeat stored locally? – Jamgold Feb 04 '11 at 23:21
  • 1
    just in case anyone doesn't understand, this is the "heartbeat" Jamgold is referring to. I only comment to help others in their quest ;) http://stackoverflow.com/questions/4940657/handling-standby-on-ipad-using-javascript/4941098#4941098 – AndyL Jun 28 '11 at 13:27
  • Awesome! The `pagehide` event is queued even when the `home` and `lock` buttons are pressed in 4.3.4, but it isn't until you reopen Safari (or unlock the device) that it gets executed. I don't think it gets any better than this. It's good enough for me. – fregante Jul 24 '11 at 01:44