Hello Team,
How can I identify if an iOS app has been terminated by the User or by iOS?
As per the documentation: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminatewill get called irrespective of the fact the app has been killed by the User or by iOS.
Also, to note I'm not using any UIBackgroundModes in my application.
I've got a requirement where I want to save some data in NSUserDefaults only if the System(iOS) has terminated the app. Is there any way I can determine that?
Thanks!
OK, that may be possible. The basic strategy would be to record in persistent storage when you app moves between the background and foreground state. If your app is launched and your last record indicates it was in the foreground state, then somehow it was terminated unexpectedly.
There’s a bunch of gotchas to watch out for here:
The above assumes you’re not running in the background. If you can run in the background then you have to substitute becomes eligible for suspension for moves to the background, and becomes ineligible for suspension for moves to the foreground.
Last I checked
is called when the user terminates your app from the multitasking UI while your app is in the foreground. You should test this case and handle it explicitly.-applicationWillTerminate:
Don’t store your flag in
, but rather as a simple file on disk. Specifically, you should use the presence or absence of that file as the flag, rather than the content.NSUserDefaults
Note User defaults are meant for general-purpose preferences, not for situations like this, where you need to understand the synchronisation semantics of your persistent storage.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"