xcode iPhone 应用程序终止时调用哪个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10200505/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:00:03  来源:igfitidea点击:

Which function is called when iPhone app is terminated?

iphonexcodeios5

提问by Max

I'm working on a project in Xcode version 4.2.1 and in iOS 5.0. I'm wondering which function will be called when you completely terminate an application, which means even the time when an users force the app running on background to be terminated from home screen. I was assuming that

我正在 Xcode 版本 4.2.1 和 iOS 5.0 中开发一个项目。我想知道当您完全终止应用程序时会调用哪个函数,这意味着即使用户强制在后台运行的应用程序从主屏幕终止时也是如此。我假设

(void)applicationWillTerminate:(UIApplication *)application

(void)applicationWillTerminate:(UIApplication *)application

would be called, but it turned out it was not called even after terminating the app on background. What I basically want to do is to call a method that I created just before the app is completely terminated, so need to know which function is called at the end of the app.

将被调用,但事实证明即使在后台终止应用程序后它也没有被调用。我基本上想做的是在应用程序完全终止之前调用我创建的方法,因此需要知道在应用程序结束时调用了哪个函数。

Thank you.

谢谢你。

回答by Nitesh Borad

"applicationWillTerminate" method will be called only when application is not in suspended statewhile being terminated.

applicationWillTerminate”方法仅在应用程序终止时不处于挂起状态时才会调用。

To understand this, considering currently your application is in foreground active state, go through following two cases:

要理解这一点,考虑到您的应用程序当前处于前台活动状态,请通过以下两种情况:

Case 1:When you single tapHome button, app directly goes to suspended state by calling methods - (1) applicationWillResignActive and then (2) applicationDidEnterBackground.

案例1:当您单击主页按钮时,应用程序通过调用方法直接进入挂起状态 - (1) applicationWillResignActive 然后 (2) applicationDidEnterBackground。

Now when you try to quit/terminate app, by double tapping home button and then swiping up the current app screen from recent app screens, app is in suspended state. So, "applicationWillTerminate" method will not be called.

现在,当您尝试退出/终止应用程序时,通过双击主页按钮,然后从最近的应用程序屏幕向上滑动当前应用程序屏幕,应用程序处于暂停状态。因此,不会调用applicationWillTerminate”方法。

Case 2:When you double tapHome button, app goes to inactive state by calling method - (1) applicationWillResignActive.

情况 2:当您双击主页按钮时,应用程序通过调用方法 - (1) applicationWillResignActive 进入非活动状态。

Now when you try to quit/terminate app, by swiping up the current app screen from recent app screens, app is in inactive state (not in suspended state) while being terminated. So, "applicationWillTerminate" method will be called.

现在,当您尝试退出/终止应用程序时,通过从最近的应用程序屏幕向上滑动当前应用程序屏幕,应用程序在终止时处于非活动状态(不是挂起状态)。因此,将调用applicationWillTerminate”方法。

Look what Apple say: enter image description here

看看苹果怎么说: 在此处输入图片说明

For more info on this look - Apple's Official Documentation on applicationWillTerminate(_:)

有关此外观的更多信息 - Apple 关于 applicationWillTerminate(_:) 的官方文档

回答by rickster

Here's a good overviewof the application lifecycle notifications & delegate messages on iOS 4.0 and newer. In short...

这是对 iOS 4.0 和更新版本上的应用程序生命周期通知和委托消息的一个很好的概述。简而言之...

Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.

您的应用程序通常永远不会看到willTerminate,因为系统通常只会在您的应用程序已经暂停(在后台)后终止它。一旦你的应用被暂停,它就没有机会采取行动(*),所以没有回调。

The didEnterBackgrounddelegate message or notification should be considered your last chance to clean things up or save state before possible termination.

didEnterBackground代表消息或通知,应考虑您的最后一次机会,清理有害内容或保存状态之前可能终止。



(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.

(*) 好的,如果您的应用程序处于受支持的后台执行模式之一(例如音频、VoIP 或导航),则它可以执行某些操作,但在这种情况下,它要么尚未暂停,要么已通过条目取消暂停特定于该背景模式的点。

回答by OldPeculier

If the app is already suspended, it will not receive further notifications. Watch for the didEnterBackgroundnotification.

如果应用程序已经暂停,它将不会收到进一步的通知。留意didEnterBackground通知。

回答by danh

- (void)applicationWillTerminate:(UIApplication *)application

It's in the UIApplicationDelegate protocol.

它在 UIApplicationDelegate 协议中。

回答by Art Gillespie

The documentation herespells out the behavior for this case.

此处文档详细说明了这种情况下的行为。

In short, you'll only receive the applicationWillTerminate:message if the app is running (either in the foreground or background) when it's terminated. So, unless your app is running in the foreground or in one of the long-running background modes (e.g. VoIP, Audio, or a long-running task indicated by beginBackgroundTaskWithExpirationHandler) it will transition from the backgroundstate to the suspendedstate fairly quickly. In this case, you'll never receive the applicationWillTerminate:message.

简而言之,您只会applicationWillTerminate:在应用程序终止时正在运行(在前台或后台)时收到消息。因此,除非您的应用程序在前台或长时间运行的后台模式之一(例如 VoIP、音频或由 指示的长时间运行的任务beginBackgroundTaskWithExpirationHandler)中运行,否则它将相当快地从background状态转换到suspended状态。在这种情况下,您将永远不会收到该applicationWillTerminate:消息。