iOS call Storage.set when app closes

Based on the documentation for cordova events and the Platform class I conclude that there is no way to intercept my app closing (you know when you double click home button and swipe up to kill the app) on iOS.

In my app I would like to perform'endTime',new Date()) but cannot think of a way to accomplish this.

One way would be to call this x second and as it stops updating because the app is not running any more, you will know the last active value on next start. But this will cost your users lots of battery and they will probably hate your app.

A less user hostile way would be to do this on every interaction in your app. This is a better metric anyway, because you don’t want to know the user had your app open for 4 hours but did nothing (I assume).

A even less hostile way on every significant interaction. You want to know this because of some reason, which is probably when the user last did something (I assume).

Depends which version of ionic you’re using.

If you’re using the newest with typescript; couldn’t you do:

ionViewWillLeave() {
console.log("user is leaving");


ionViewWillLeave isn’t triggered when your app closes, only when you navigate to another page.

1 Like

Ah… So then he’ll have to figure it out from his back-end or some other solution. Hm, will the service-workers work? They’re pretty active keeping tabs on the apps state.

Thanks for the responses. I am currently doing this cludge

 setInterval(() => {
      }, 5000);

FYI this app is basically maintaining several stop watches which are capturing end times. the app has low accuracy requirement so is why I set to 5 seconds.

1 Like

That’s what I was thinking. That’s how my back-end keeps tabs on my users online activity. That’s probably the only solution.

I guess it depends on what you mean by “back-end”, but with a normal REST server the back-end itself can keep track of timestamps itself without the app having to do anything.

One thing that might be worth at least investigating is whether Platform.pause() fires at this point.

Platform.pause is not called.

1 Like