如何知道应用程序是否在 iOS 后终止?

How to know whether an app is being terminating in iOS?

- (void)applicationDidEnterBackground:(UIApplication *)application {
    //...function_a call    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    //...function_b call    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    //...function_c call    
}

我正在使用 AppDelegates 方法跟踪我的应用程序。它工作正常,如果应用程序只是从后台转到前台。但是,当我尝试从 运行 应用程序中删除我的应用程序时,它将首先调用 - (void)applicationDidEnterBackground:(UIApplication *)application,然后调用 - (void)applicationWillTerminate:(UIApplication *)application。我如何在 - (void)applicationDidEnterBackground:(UIApplication *)application 中知道该应用程序正在终止。意思是,我不希望 function_a 调用两次,而应该只在应用程序进入后台时调用。

更新:

即使设置跟随观察者也无济于事。

UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillTerminateNotification

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

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.

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.

因此,这是 to go 委托方法,用于了解应用程序何时将被用户或系统终止,而 - (void)applicationWillTerminate:(UIApplication *)application 运行时应用程序进入后台,这并不意味着它已退出,除非您的退出意味着不可见