Search code examples
javascriptiphoneiosonunload

How can I detect shutdown of a cached iPhone app?


I have an iPhone webapp that uses a cache manifest to work offline and add the webapp to my Home screen. What I would like to find is a way to detect that the app is exiting so I can do some housekeeping and save some data.

If I am running as a web page in Safari, window.onunload does this for me, but this event does not happen when running from the Home screen.


Solution

  • 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 :-("