当我的应用程序在ios中的后台终止时如何执行代码

时间:2022-12-29 16:58:43

In iOS, we all know that there is AppDelegate method applicationWillTerminate, and it is called when my app is closed by user when it is currently running(i.e. not in background). But I want to do something(save data, for example) when my app is terminated(closed by user or killed by OS) when it runs in background.

在iOS中,我们都知道AppDelegate方法是applicationWillTerminate,当用户当前正在运行我的应用程序时(即不在后台),它会被调用。但是当我的应用程序在后台运行时终止(由用户关闭或被操作系统杀死)时,我想做某事(例如保存数据)。

PS: my app can run in background.

PS:我的应用可以在后台运行。

Do you have any solutions? thanks.

你有什么解决办法?谢谢。

1 个解决方案

#1


6  

Sorry but you should use applicationWillTerminate:

抱歉,您应该使用applicationWillTerminate:

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.

对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,当用户退出应用程序时,始终会调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下应用程序只是移动到后台。但是,可以在应用程序在后台运行(未暂停)并且系统由于某种原因需要终止它的情况下调用此方法。

So if you need to save data ALSO when user manually kill the app use applicationDidEnterBackground that it's called if your app support background mode.

因此,如果您需要在用户手动终止应用程序时保存数据,请使用applicationDidEnterBackground,如果您的应用程序支持后台模式,则调用它。

#1


6  

Sorry but you should use applicationWillTerminate:

抱歉,您应该使用applicationWillTerminate:

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.

对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,当用户退出应用程序时,始终会调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下应用程序只是移动到后台。但是,可以在应用程序在后台运行(未暂停)并且系统由于某种原因需要终止它的情况下调用此方法。

So if you need to save data ALSO when user manually kill the app use applicationDidEnterBackground that it's called if your app support background mode.

因此,如果您需要在用户手动终止应用程序时保存数据,请使用applicationDidEnterBackground,如果您的应用程序支持后台模式,则调用它。