Reputation: 126367
While the device is powered on, is it possible for iOS to automatically terminate my app (calling applicationWillTerminate:
) while it's in the background?
I'm also curious what happens in two other cases, three in total:
I'm asking because I want to know how often applicationWillTerminate:
is likely to get called. I want to know this because that's where I'm registering for remote notifications. And if there's a failure sending the device token to the server, I want to know how likely it is that that method will get called again (i.e., retry sending the device token to the server).
Upvotes: 1
Views: 6550
Reputation: 126367
Check out iOS Developer Library : iOS App Programming Guide : App Termination.
Upvotes: 1
Reputation: 125007
Not only can iOS terminate your app automatically, but the user can kill it manually. In fact, the only time the user can kill your app is when it's in the background. Furthermore, when your app is "in the background" it's more likely to be suspended than actually running, so don't count on doing a lot of processing when you're not the foreground app.
As for how likely it is that you'll get -applicationWillTerminate:
, that'll depend on the user and how they're using their device. You should handle it appropriately when you get it, and go about your business otherwise.
Upvotes: 5
Reputation: 78363
If your application supports multitasking (the default for anything linked against iOS 4.0+), this method will almost never be called. The documentation says it may be called in cases where the application is running in the background and the system wants to terminate. However, in my experience, I've only ever seen this actually called when running a music app that's actively playing music in the background and the system is jettisoning everything. In cases where I have background tasks running (not music, but short-term background tasks), I've seen the app terminated without this method being called.
I wouldn't ever rely on this being called and try and do all the clean-up you need to do in your delegate methods for transitioning into the background and your background task completion blocks (which do get executed for at least a few seconds before the app gets jettisoned).
Upvotes: 7
Reputation: 27835
When memory is running low, iOS can shut down your app, calling applicationWillTerminate
.
The docs say this about the method:
... However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
Upvotes: 3