ios 什么时候会调用 applicationWillTerminate?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29416375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When will applicationWillTerminate be called?
提问by Boon
In what situations will applicationWillTerminate be called? For example, will it ocassionally be called if there is a crash in the code?
在什么情况下会调用 applicationWillTerminate?例如,如果代码中出现崩溃,它会偶尔被调用吗?
Apple's doc is vague on this, it only says when the system needs to terminate it for some reason.
Apple 的文档对此含糊其辞,仅说明系统何时因某种原因需要终止它。
For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
对于不支持后台执行或链接 iOS 3.x 或更早版本的应用程序,当用户退出应用程序时始终调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下应用程序只会移动到后台。但是,在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可能会调用此方法。
回答by Alex
I have just explored this question (iOS 9.2). And I have got some results.
我刚刚探讨了这个问题(iOS 9.2)。我已经得到了一些结果。
So, applicationWillTerminate
is called when a user terminates the app without switching it to background mode: the app is active, the user makes double press on Home button and throws out the app.
因此,applicationWillTerminate
当用户终止应用程序而不将其切换到后台模式时调用:应用程序处于活动状态,用户双击主页按钮并抛出应用程序。
But if a user switches the app to the background at first, and then after this tries to terminate the app, applicationWillTerminate
will not be called.
但是如果用户首先将应用程序切换到后台,然后在此之后尝试终止应用程序,applicationWillTerminate
则不会被调用。
You can check this:
你可以检查这个:
- (void)applicationWillTerminate:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"term"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and
和
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"term"]){
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"term"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WORKED" message:@"term works" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
}
...
return YES;
}
}
回答by Duncan C
Starting in iOS 4, the practical answer is "never." (or at least "rarely".) You can't assume that it will ever be called. Normally what happens is that your app gets moved to the background when the user presses the home button and then a few seconds later it shifts to the "suspended state" (Still in memory but not receiving any CPU time.)
从 iOS 4 开始,实际的答案是“从不”。(或者至少“很少”。)你不能假设它会被调用。通常发生的情况是,当用户按下主页按钮时,您的应用程序会移至后台,然后几秒钟后它会切换到“挂起状态”(仍在内存中但未接收任何 CPU 时间。)
Once your app is in the suspended state the system can terminate it at any time without warning (usually due to memory pressure.)
一旦您的应用程序处于挂起状态,系统可以随时终止它而不会发出警告(通常是由于内存压力)。
When you are suspended you don't get the applicationWillTerminate call before being killed. You should assume that when you get a applicationDidEnterBackground:
message, you are going to be suspended shortly after, and die while suspended. Get your affairs in order (save app state.)
当您被暂停时,您不会在被杀死之前收到 applicationWillTerminate 调用。你应该假设当你收到一条applicationDidEnterBackground:
消息时,你很快就会被暂停,并在暂停时死亡。让您的事务井然有序(保存应用程序状态。)
You may still get calls to your applicationWillTerminate in certain cases, but you should not assume that you will.
在某些情况下,您可能仍会收到对 applicationWillTerminate 的调用,但您不应假设您会调用。
回答by jk7
App Termination according to the App Programming Guide for iOS:
根据iOS 应用程序编程指南终止应用程序:
- Apps must be prepared for termination to happen at any time and should not wait to save user data or perform other critical tasks.
- System-initiated termination is a normal part of an app's life cycle. The system usually terminates apps so that it can reclaim memory and make room for other apps being launched by the user, but the system may also terminate apps that are misbehaving or not responding to events in a timely manner.
- Suspended apps receive no notification when they are terminated; the system kills the process and reclaims the corresponding memory.
- If an app is currently running in the background and not suspended, the system calls the applicationWillTerminate:of its app delegate prior to termination.
- The system does not call this method when the device reboots.
- In addition to the system terminating your app, the user can terminate your app explicitly using the multitasking UI. User-initiated termination has the same effect as terminating a suspended app. The app's process is killed and no notification is sent to the app.
- 应用程序必须做好随时终止的准备,不应等待保存用户数据或执行其他关键任务。
- 系统启动的终止是应用程序生命周期的正常部分。系统通常会终止应用程序,以便它可以回收内存并为用户启动的其他应用程序腾出空间,但系统也可能会终止行为不端或未及时响应事件的应用程序。
- 暂停的应用在终止时不会收到通知;系统杀死进程并回收相应的内存。
- 如果应用程序当前正在后台运行且未挂起,则系统会在终止之前调用其应用程序委托的applicationWillTerminate:。
- 设备重启时系统不会调用该方法。
- 除了系统终止您的应用程序之外,用户还可以使用多任务 UI 显式终止您的应用程序。用户发起的终止与终止暂停的应用程序具有相同的效果。应用程序的进程被终止,并且不会向应用程序发送通知。
回答by Kyle Howells
When a user has the app open and then presses the home button iOS doesn't close the app but instead suspends it and puts it in the background.
当用户打开应用程序然后按下主页按钮时,iOS 不会关闭应用程序,而是将其挂起并将其置于后台。
However, iOS devices only have 1GB of ram (mostly) so after opening and closing a few apps they are starting to run out.
但是,iOS 设备只有 1GB 的内存(大部分),因此在打开和关闭一些应用程序后,它们开始耗尽。
iOS now has to kick some of those apps out of ram. So it wakes up (at a guess the biggest ram user, or oldest used app) an app and tells it to save anything it needs to close down gracefully.
iOS 现在必须从 ram 中删除其中一些应用程序。所以它唤醒(猜测最大的 ram 用户,或最旧的使用应用程序)一个应用程序,并告诉它保存任何它需要优雅地关闭。
That's when -applicationWillTerminate:
is called. When iOS is closing down your app. Of course if you block that method from returning for too long iOS will just kill your app anyway (it needs to resources after all).
那个时候-applicationWillTerminate:
叫。当 iOS 关闭您的应用程序时。当然,如果您阻止该方法返回太久,iOS 无论如何都会杀死您的应用程序(毕竟它需要资源)。
If you are saving everything in -willResignActive:
and -willEnterBackground:
then you can pretty much just ignore the method and your app will close after the method returns.
如果您正在保存所有内容-willResignActive:
,-willEnterBackground:
那么您几乎可以忽略该方法,并且您的应用程序将在该方法返回后关闭。
EDIT: If the user tells the app switcher to close an app it will also try to gracefully close your app down. But if it is a situation where the device needs more resources there's a chance you won't get -applicationWillTerminate:
called, as the device won't have time to as it might need the resources faster than your app can be told to gracefully close. So applicationWillTerminate:
isn't guaranteed to be called.
编辑:如果用户告诉应用程序切换器关闭应用程序,它也会尝试正常关闭您的应用程序。但是,如果设备需要更多资源,则您可能不会被-applicationWillTerminate:
调用,因为设备没有时间调用资源,因为它可能需要资源的速度比您的应用程序被告知正常关闭的速度要快。所以applicationWillTerminate:
不能保证被调用。
回答by Gil Sand
I think it will be called when the user kills the app from foreground or when the OS does it himself. the rest of the time it's just a big "maybe". I usually don't rely on that because of that "maybe".
我认为它会在用户从前台杀死应用程序或操作系统自己执行时调用。其余的时间只是一个很大的“可能”。我通常不依赖那个,因为那个“也许”。
You can use the "didEnterBackground" which is called in a more reliable way
您可以使用以更可靠的方式调用的“didEnterBackground”
You can also try to create a crash on purpose and see how it reacts exactly (crash from foreground & background).
您还可以尝试故意创建崩溃并查看它的准确反应(从前台和后台崩溃)。
回答by Zgpeace
In swift, use delegate in the below
在 swift 中,在下面使用委托
optional func applicationWillTerminate(_ application: UIApplication)
This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
此方法让您的应用程序知道它即将终止并从内存中完全清除。您应该使用此方法为您的应用程序执行任何最终清理任务,例如释放共享资源、保存用户数据和使计时器失效。您对该方法的实现有大约五秒钟的时间来执行任何任务并返回。如果该方法在时间到期之前没有返回,则系统可能会完全终止该进程。
参考:https: //developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate