管理你的应用程序的生命周期
了解应用程序代理如何管理你的应用程序的高级行为。(Understand how the app delegate manages your app's high-level behavior.)
总览
UIKit程序总是处于这五种状态之一,如图1所示。程序从未运行开始。当用户明确启动了程序,应用程序在进入活动状态前会短暂的移动到非活动状态。(一个活动的应用程序出现在屏幕上被称为前台程序。)退出一个活动的应用程序会将它移出屏幕和转入背后状态,此状态下,系统会在很短的时间后暂停该状态。系统可以自行决定终止已暂停的程序,并将它恢复到未运行状态。(UIKit apps are always in one of five states, which are shown in Figure 1. Apps start off not running. When the user explicitly launches the app, the app moves briefly to the inactive state before entering the active state. (An active app appears onscreen and is known as a foreground app.) Quitting an active app moves it offscreen and into the background state, where it stays until the system suspends it a short time later. At its discretion, the system may quietly terminate a suspended app, returning it to the not running state.)
你的应用程序的状态决定了它能的得到什么样的系统资源。因为活动状态的应用程序在屏幕上是可见的,它必须要回应用户交互,他们在使用系统资源上拥有优先权。后台程序在屏幕上是不可见的,因此,在获取系统资源上受到更多的限制,并获得有限的运行时间。(Your app’s current state defines what system resources are available to it. Because active apps are visible onscreen and must respond to user interactions, they have priority when it comes to using system resources. Background apps are not visible onscreen, and therefore have more limited access to system resources and receive limited execution time.)
管理时间生命周期(Manage Life Cycle Events)
当您的应用程序从一种状态转换到另一种状态时,UIKit会通知您的应用程序委托对象 - 符合UIApplicationDelegate协议的对象。使用您的应用程序委托来修改您的应用程序的行为以匹配其新状态。例如,从非运行状态转移到非活动状态时,处理准备运行应用程序所需的启动时任务。(When your app transitions from one state to another, UIKit notifies your app delegate object—an object that conforms to the UIApplicationDelegate protocol. Use your app delegate to modify your app's behavior to match its new state. For example, when moving from the not running to inactive state, handle the launch-time tasks needed to prepare your app to run.)
系统通知你的程序委托有关以下转换:(The system notifies your app delegate about the following transitions:)
- 启动。你的应用程序从非运行转换到非活动或后台状态。使用这个转换来准备应用程序去运行;查看对启动应用程序做响应(Launch. Your app transitions from the not running to the inactive or background state. Use this transition to prepare your app to run; see Responding to the Launch of Your App.)
- 激活。你的程序从非活动状态转换到活动状态。为你的程序运行在前台做准备并在屏幕上可见。查看为你的程序在前台运行做准备(Activation. Your app transitions from the inactive to the active state. Prepare your app to run in the foreground and be visible onscreen; see Preparing Your App to Run in the Foreground.)
- 非激活。你的应用程序从活动状态转换到非激活状态。安静你的应用程序,也许是暂时的。查看为你的程序在后台运行做准备(Deactivation. Your app transitions from the active to the inactive state. Quiet your app, perhaps only temporarily; see Preparing Your App to Run in the Background.)
- 后台执行。你的程序从非激活状态或非运行状态转换到后台状态。查看为你的应用程序运行在后台状态做准备(Background execution. Your app transitions from the inactive or not running state to the background state. Prepare to handle only essential tasks while running offscreen; see Preparing Your App to Run in the Background.)
- 终止。你的程序从任何其它运行转台转换到非运行状态。(暂停的程序在被终止时,不被通知。)取消所有的任务和准备退出。查看applicationWillTerminate:。(Termination. Your app transitions from any running state to the not running state. (Suspended apps are not notified when they are terminated.) Cancel all tasks and prepare to exit; see applicationWillTerminate:.)
管理行为事件(Manage Behavioral Events)
你的应用程序委托也回应一些其它重要的事件:(Your app delegate also responds to some other important events:)
- 减少你的应用程序使用内存量。查看响应内存警告(Memory warnings. Reduce the amount of memory your app uses; see Responding to Memory Warnings.)
- 时间发生改变。更新你的应用的时间敏感特性。(Time changes. Update time-sensitive features of your app.)
- 受保护的数据变的可用或不可用。当用户锁住或解锁设备时保护数据。(Protected data becomes available/unavailable. Manage files when the user locks or unlocks the device.)
- 状态恢复。恢复你的应用程序的UI到之前的状态,表现你的应用程序永不停止运行;参看在发布中保留应用的UI(State restoration. Restore your app’s UI to its previous state, giving the appearance that your app never stopped running; see Preserving Your App's UI Across Launches.)
- 切换任务。 继续在另一台设备上启动任务。(Handoff tasks. Continue tasks started on another device.)
- 打开网址。 接收并打开发送到您的应用的网址。(Open URLs. Receive and open URLs sent to your app.)
- 应用程序间通信。 从配对的watchOS应用接收数据。 (Inter-app communication. Receive data from a paired watchOS app.)
- 文件下载。使用NSURLSession对象接收你的应用程序下载的文件。(File downloads. Receive files that your app downloaded using a NSURLSession object.)
尽管应用委托是你处理生命周期事件最优先级的位置,但是它不是唯一的位置。大部分的事件,UIKit也可以产生任何对象可以观察的通知。有关您可以观察的与应用程序相关的通知列表,请参阅UIApplication。有关用于处理事件的方法的更多信息,请参阅UIApplicationDelegate。(Although the app delegate is the primary place where you handle life cycle events, it is not the only place. For most events, UIKit also generates notifications that any object can observe. For a list of app-related notifications that you can observe, see UIApplication. For more information about the methods you use to handle events, see UIApplicationDelegate.)
话题
生命周期循环事件(Life Cycle Events)
响应程序启动
初始化爹程序和准备它开始运行。(Initialize your app and prepare it to run.)
为你的程序在前台运行做准备
配置你的程序出现在屏幕上。(Configure your app to appear onscreen.)
为你的程序在后台运行做准备
为你的程序暂停做准备。(Prepare your app to be suspended.)
行为事件(Behavioral Events)
响应内存警告
当系统要求这样做时释放内存。(Free up memory when asked to do so by the system.)
通过发布保留应用的用户界面
在系统终止后,将您的应用程序恢复到之前的状态。(Return your app to its previous state after it is terminated by the system.)