1

我想在用户退出应用程序时保存一些应用程序状态数据。我在想我应该在我的 appDelegate 中使用 applicationWillTerminate 方法,但是当我尝试在那里做任何事情时,它什么也没做:

- (void) applicationWillTerminate:(UIApplication *)application {
NSLog(@"test");
 }

当我运行我的应用程序,做一些事情,然后点击主页按钮退出应用程序时,控制台什么也没有...

我必须在 appDelegate 中实现 applicationWillTerminate 方法吗?我的应用程序的用户很可能在他们离开时处于阅读器视图中,无论如何要在那里实现应用程序将关闭方法?

4

2 回答 2

2

see this link iPhone simulator and applicationWillTerminate() for older iOS versions;

but remember the Home button does not necessarily terminate the application is ios4... you should use applicationDidEnterBackground but would suggest both places applicationWillTerminate and applicationDidEnterBackground

Adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES will make you application quit when you hit the home button, even on iOS4

于 2010-10-29T17:07:58.963 回答
2

In iOS 4, the applicationWillTerminate: method is only called if you opt out of the background execution model. In the standard iOS 4 application lifecycle, applications are suspended (not terminated) when the home button is pressed. Your application delegate will receive an applicationWillResignActive: message followed by an applicationDidEnterBackground: message. The applicationDidEnterBackground: method is a good place to save user data.

于 2010-10-29T17:12:09.980 回答