开发者

How can I determine when my application is about to be terminated in the background?

When my application is moved into the background, I'd like to be able to detect when it is about to be terminated (for memory exhaustion or ot开发者_开发问答her reasons). Is there a way to do this?

In particular, is there a way to execute some code before the application is terminated when in the background?


You can do this in the -[<UIApplicationDelegate> applicationWillTerminate:] method of your application delegate, like this:

- (void)applicationWillTerminate:(UIApplication *)application {
    [database save]; // or whatever you want to do
}

This will be executed whenever the app is about to be terminated, unless it crashes.


Your best bet is to do whatever cleanup needs to be done in your application (saving state or user data, etc.) as your application transitions into the background. If your application is suspended, you will not have a chance to perform any last code before it is terminated by the system.

From the iOS Application Programming Guide:

If your application is running (either in the foreground or background) at termination time, the system calls your application delegate’s applicationWillTerminate: method so that you can perform any required cleanup. You can use this method to save user data or application state information that you would use to restore your application to its current state on a subsequent launch. Your method implementation has approximately 5 seconds to perform any tasks and return. If it does not return in time, the application is killed and removed from memory. The applicationWillTerminate: method is not called if your application is currently suspended.

Even if you develop your application using iOS SDK 4 and later, you must still be prepared for your application to be killed without any notification. The user can kill applications explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove applications from memory to make more room. If your application is currently suspended, the system kills your application and removes it from memory without any notice. However, if your application is currently running in the background state (in other words, not suspended), the system calls the applicationWillTerminate: method of your application delegate. Your application cannot request additional background execution time from this method.


It depends.

If you mean getting notification while your application is suspended in the background, there is no way to know; the applicationWillTerminate: method is not run if you're suspended. The recommended approach is to save any required state when you get the applicationWillEnterBackground: message, so that if you get killed in the background you're ready to start up again.

If you're actually in an "executing in the background" state, (which can happen briefly after exiting the app or if the app has requested temporary background execution time,) then applicationWillTerminate: will be called just like you'd expect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜