简体   繁体   English

ApplicationWillTerminate在iOS 4.0中

[英]ApplicationWillTerminate in iOS 4.0

The applicationWillTerminate delegate method is not getting called in iOS 4.0 When I hit the Home button I am seeing the applicationWillResignActive and applicationDidEnterBackground delegate methods getting called. 在iOS 4.0中没有调用applicationWillTerminate委托方法当我点击Home按钮时,我看到applicationWillResignActive和applicationDidEnterBackground委托方法被调用。

 - (void)applicationWillResignActive:(UIApplication *)application
{
  NSLog(@"Application Did Resign Active");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  NSLog(@"Application Did Enter Background");
}

And when I double Tap the Home button and again launch the Application the i find the applicationWillEnterForeground and applicationDidBecomeActive delegate methods are getting called. 当我双击主页按钮并再次启动应用程序时,我发现applicationWillEnterForeground和applicationDidBecomeActive委托方法被调用。

 - (void)applicationWillEnterForeground:(UIApplication *)application
{
  NSLog(@"Application Will Enter Foreground");
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
  NSLog(@"Application Did Become Active");
}

But I want to know when the applicationWillTerminate delegate method will be called , where I do some DB/file backup routines. 但我想知道何时调用applicationWillTerminate委托方法,在那里我做一些DB /文件备份例程。

- (void)applicationWillTerminate:(UIApplication *)application{

}

I even tried to hit the minus sign and deleted the App running in the Background , but still it did not call any delegate method. 我甚至试图点击减号并删除在后台运行的应用程序,但它仍然没有调用任何委托方法。

Any Ideas ??? 有任何想法吗 ???

From the iPhone Application Programming Guide: 来自iPhone应用程序编程指南:

Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. 即使您使用iPhone SDK 4及更高版本开发应用程序,您仍必须为应用程序终止做好准备。 If memory becomes constrained, the system might remove applications from memory in order to make more room. 如果内存受限制,系统可能会从内存中删除应用程序以腾出更多空间。 If your application is currently suspended, the system removes your application from memory without any notice. 如果您的应用程序当前处于暂停状态,系统会在不事先通知的情 However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate. 但是,如果您的应用程序当前正在后台运行,则系统会调用applicationWillTerminate:程序委托的applicationWillTerminate:方法。 Your application cannot request additional background execution time from this method. 您的应用程序无法从此方法请求额外的后台执行时间。

So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: and applicationDidEnterBackground: . 所以,是的, applicationWillTerminate:通常不会在iOS 4中经常调用。如果你必须保存数据,你应该在applicationWillTerminate:applicationDidEnterBackground: .

在iPhone OS采用多任务的WWDC 2010会议( 第2部分 )解释了应用程序状态转换非常好。

I got one solution for terminating apps when user hits the Home button in iOS4. 当用户点击iOS4中的Home按钮时,我有一个终止应用程序的解决方案。 This will call the applicationWillTerminate delegate method instead of entering into background process. 这将调用applicationWillTerminate委托方法,而不是进入后台进程。

  1. Open your info.plist file 打开info.plist文件
  2. Add The Key UIApplicationExitsOnSuspend 添加密钥UIApplicationExitsOnSuspend
  3. Set the new key to YES 将新键设置为YES

Actually you can also use this step to do so. 实际上您也可以使用此步骤来执行此操作。

  1. Open your info.plist file 打开info.plist文件
  2. Add The Key -> Application does not run in background 添加密钥 - >应用程序不在后台运行
  3. Set this key value to YES 将此键值设置为YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM