I tested the pagehide event using the below code and found that it works well for detecting whether the user navigated to another link or simply opened a new tab when you are in safari.
However, if you are in a web app saved to your homescreen (like you describe) then the pagehide event is useless for telling if the web app was closed.
Depending on what you need specifically, you can work around this limitation by saving data to localStorage and then checking the localStorage when the app opens again. Then you can perform any extra work that may need to be done before the app fully starts again.
function myLoadHandler(evt)
{
if (evt.persisted) {
alert('user returns to page from another tab');
return;
}
alert('loading new page');
}
function myUnloadHandler(evt)
{
if (evt.persisted) {
alert('user goes to new tab');
return;
}
alert('user truly leaves the page');
}
if ("onpagehide" in window) {
window.addEventListener("pageshow", myLoadHandler, false);
window.addEventListener("pagehide", myUnloadHandler, false);
} else {
window.addEventListener("load", myLoadHandler, false);
window.addEventListener("unload", myUnloadHandler, false);
}