如何执行code时,我的应用程序在后台终止的ios [英] how to execute code when my app terminated in background in ios

查看:171
本文介绍了如何执行code时,我的应用程序在后台终止的ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS上,大家都知道有AppDelegate中的方法 applicationWillTerminate ,当我的应用程序被用户时,它正在运行关闭它被称为(即不是在后台)。但是,我想要做的事(保存数据,例如)时,我的应用程序被终止(由用户关闭或杀害OS),当它在后台运行。

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.

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

PS: my app can run in background.

你有什么办法呢?谢谢。

Do you have any solutions? thanks.

推荐答案

很抱歉,但你应该使用 applicationWillTerminate

Sorry but you should use 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.

对于不支持后台执行或链接应用
  反对的iOS 3.x或更早,此方法总是叫用户时
  退出应用程序。 对于支持后台运行的应用程序, 这个方法
  当用户退出应用程序,因为应用程序通常不叫
  简单地移动到在这种情况下的背景。然而,这种方法可能
  在该应用是在后台运行的情况下,被称作
  (不暂停)和系统需要终止它的某些原因。

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.

因此​​,如果您还需要保存数据时,用户手动终止该应用的使用也 applicationDidEnterBackground ,这就是所谓的,如果你的应用程序支持后台运行模式。

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

这篇关于如何执行code时,我的应用程序在后台终止的ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆