iOS文档--Managing Your App's Life Cycle 管理app的生命周期

Article

Managing Your App's Life Cycle

Respond to system notifications when your app is in the foreground or background, and handle other significant system-related events.

        --文章描述了 响应app处于前台或后台时的系统通知,或者处理其他重要的系统相关事件。

 

Overview          --概览

 

The current state of your app determines what it can and cannot do at any time. For example, a foreground app has the user’s attention, so it has priority over system resources, including the CPU. By contrast, a background app must do as little work as possible, and preferably nothing, because it is offscreen. As your app changes from state to state, you must adjust its behavior accordingly.

       --app的当前状态决定了它在什么时候可以做什么和不能做什么。例如,前台app拥有用户的注意力,因此它的优先级高于系统资源,包括CPU资源。相比之下,后台应用程序必须尽可能少做工作,最好什么也不做,因为它是在屏幕外的。当你的应用程序从一个状态更改到另一个状态时,你必须相应地调整它的行为。

When your app’s state changes, UIKit notifies you by calling methods of the appropriate delegate object:

       --当app的状态发生改变时,UIKit通过调用恰当的委托对象的方法通知你

  • In iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app.
        --在iOS 13及更高版本中,UIKit使用UISceneDelegate对象响应基于scene的app中的生命周期事件。

  • In iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events.
        --在iOS 12及更早版本中,UIKit使用UIApplicationDelegate对象响应生命周期事件。

Note

If you enable scene support in your app, iOS always uses your scene delegates in iOS 13 and later. In iOS 12 and earlier, the system uses your app delegate.

       --如果版本支持的话,系统默认采用scene委托来管理生命周期。

 

 

Respond to Scene-Based Life-Cycle Events       --响应基于scenes的生命周期事件

 

If your app supports scenes, UIKit delivers separate life-cycle events for each. A scene represents one instance of your app’s UI running on a device. The user can create multiple scenes for each app, and show and hide them separately. Because each scene has its own life cycle, each can be in a different state of execution. For example, one scene might be in the foreground while others are in the background or are suspended.

       --如果你的app支持scenes,UIKit会为每个scene提供单独的生命周期事件。一个scene表示在设备上运行的app UI的一个实例用户可以为一个app创建多个scene,并分别显示和隐藏它们。因为每个scene都有自己的生命周期,每个场景都可能处于不同的执行状态。例如,一个场景可能在前台中,而其他scene在后台中或被挂起。

Important

Scene support is an opt-in feature. To enable basic support, add the UIApplicationSceneManifest key to your app’s Info.plist file as described in Specifying the Scenes Your App Supports.

         --scenes支持是一个可选功能。要启用scenes的基本支持,请将UIApplicationSceneManifest密钥添加到app的Info.plist文件中,如超链接所述。

The following figure shows the state transitions for scenes. When the user or system requests a new scene for your app, UIKit creates it and puts it in the unattached state. User-requested scenes move quickly to the foreground, where they appear onscreen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. When the user dismisses your app's UI, UIKit moves the associated scene to the background state and eventually to the suspended state. UIKit can disconnect a background or suspended scene at any time to reclaim its resources, returning that scene to the unattached state.

          --下图显示了scene的状态转换。当用户或系统为app请求新scene时,UIKit会创建该scene并将其置于未连接状态用户请求的scene就会快速移动到前台,并显示在屏幕上。系统请求的scene通常会移动到后台,以便处理事件。例如,系统可能在后台启动scene以处理位置事件。当用户关闭app的用户界面时,UIKit会将关联的scene移到后台状态,并最终移到挂起状态。UIKit可以随时断开后台或挂起状态的scene的连接以回收其资源,从而将该scene返回到未连接状态。

024b99c5-4ab6-4ee0-bb41-6e6426ec6a64.pnguploading.4e448015.gif转存失败重新上传取消An illustration showing the state transitions for a scene-based app. Scenes start in the unattached state and move to the foreground-active or background state. The foreground-inactive state acts as a transition state.

Use scene transitions to perform the following tasks:
         --你可以利用scene的转换状态来执行以下任务

  • When UIKit connects a scene to your app, configure your scene’s initial UI and load the data your scene needs.
          --当UIKit将一个scene连接到你的app时,配置你的scene的初始UI并加载你的scene所需要的数据。

  • When transitioning to the foreground-active state, configure your UI and prepare to interact with the user. See Preparing Your UI to Run in the Foreground.
          --当scene转换到前台活动状态时,配置您的UI并准备与用户交互。请参见超链接

  • Upon leaving the foreground-active state, save data and quiet your app’s behavior. See Preparing Your UI to Run in the Background.
           --离开前台活动状态后,保存数据并收敛app的行为。请参阅超链接:准备在后台运行时的UI。

  • Upon entering the background state, finish crucial tasks, free up as much memory as possible, and prepare for your app snapshot. See Preparing Your UI to Run in the Background.
            --进入后台状态后,完成关键任务,释放尽可能多的内存,并准备应用程序快照。请参阅超链接:准备在后台运行时的UI。

  • At scene disconnection, clean up any shared resources associated with the scene.
           --在scene断开时,清理所有与scene相关的共享资源。

  • In addition to scene-related events, you must also respond to the launch of your app using your UIApplicationDelegate object. For information about what to do at app launch, see Responding to the Launch of Your App.
           --除了与scene相关的事件外,你还必须再使用UIApplicationDelegate对象来响应app的启动。有关在app启动时执行操作的信息,请参阅超链接:响应app的启动。

 

 

Respond to App-Based Life-Cycle Events               --响应基于app委托的生命周期

 

In iOS 12 and earlier, and in apps that don't support scenes, UIKit delivers all life-cycle events to the UIApplicationDelegate object. The app delegate manages all of your app’s windows, including those displayed on separate screens. As a result, app state transitions affect your app's entire UI, including content on external displays.

       --在iOS 12和更早版本中,以及在不支持scene的app中,UIKit将所有的生命周期事件都传递给UIApplicationDelegate对象。app delegate(即UIApplicationDelegate对象管理app的所有窗口,包括在单独屏幕上显示的窗口。因此,app的状态转换会影响app的整个UI,包括外部显示的内容。

The following figure shows the state transitions involving the app delegate object. After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear onscreen. When launching to the foreground, the system transitions the app to the active state automatically. After that, the state fluctuates between active and background until the app terminates.

          --下图显示了涉及app delegate对象的状态转换。启动后,系统会将app置于非活动状态后台状态,具体取决于用户界面是否即将出现在屏幕上。当启动到前台时,系统会自动将app转换到活动状态。之后,状态在活动和后台之间波动,直到app终止。

An illustration showing the state transitions for an app without scenes. The app launches into the active or background state. An app transitions through the inactive state.

Use app transitions to perform the following tasks:
       --你可以利用状态之间的转换来执行以下操作:

  • At launch, initialize your app’s data structures and UI. See Responding to the Launch of Your App.
           --启动时,初始化app的数据结构和用户界面。请参阅超链接:响应app的启动。

  • At activation, finish configuring your UI and prepare to interact with the user. See Preparing Your UI to Run in the Foreground.
           --激活时,完成用户界面的配置并准备与用户交互。请参见超链接:准备UI在前台运行。

  • Upon deactivation, save data and quiet your app’s behavior. See Preparing Your UI to Run in the Background.
           --失活后,保存数据并钝化app的行为。请参阅超链接:准备UI在后台运行。

  • Upon entering the background state, finish crucial tasks, free up as much memory as possible, and prepare for your app snapshot. See Preparing Your UI to Run in the Background.
            --进入后台状态后,完成关键任务,释放尽可能多的内存,并准备应用程序快照。请参阅超链接:准备UI在后台运行。

  • At termination, stop all work immediately and release any shared resources. See applicationWillTerminate(_:).
           --终止时,立即停止所有工作并释放所有共享的资源。请参阅超链接: applicationWillTerminate(_:)

 

 

Respond to Other Significant Events             --响应其他(生命事件以外的)重大事件

 

In addition to handling life-cycle events, apps must also be prepared to handle the events listed in the following table. Use your UIApplicationDelegate object to handle most of these events. In some cases, you may also be able to handle them using notifications, allowing you to respond from other parts of your app.

            --除了处理生命周期事件外,app还必须准备好处理 下表列出的 事件。使用UIApplicationDelegate对象来处理大多数这些事件。在某些情况下,您还可以使用通知处理它们,从而你可以从app的其他部分进行响应这些事件,而不是非要app delegate对象响应。

Memory warnings
--内存警告

Received when your app’s memory usage is too high. Reduce the amount of memory your app uses; see Responding to Memory Warnings.
      --当内存过高时,app delegate会接收到该通知

Protected data becomes available/unavailable

  --受保护的数据变得可用/不可用

Received when the user locks or unlocks their device. See applicationProtectedDataDidBecomeAvailable(_:) and applicationProtectedDataWillBecomeUnavailable(_:).
     --当用户锁定或解锁设备时会接收到该通知

Handoff tasks
  --任务切换

Received when an NSUserActivity object needs to be processed. See application(_:didUpdate:).
    --在需要处理NSUserActivity对象时会接收到通知。参考超链接

Time changes
  --关键时间点变更

Received for several different time changes, such as when the phone carrier sends a time update. See applicationSignificantTimeChange(_:).
--特定的几个不同的时间点发生变化时接收通知,例如当电话运营商发送一个时间更新。

Open URLs
   --打开网址时

Received when your app needs to open a resource. See application(_:open:options:).
    --当你的app需要打开一个资源时,接收到通知。参考超链接

 

Topics                                                         --专题

Behavioral Events                  --行为事件

 

Responding to Memory Warnings

Free up memory when asked to do so by the system.

 

See Also

Life Cycle

 

Responding to the Launch of Your App

Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.

class UIApplication

The centralized point of control and coordination for apps running in iOS.

protocol UIApplicationDelegate

A set of methods that you use to manage shared behaviors for your app.

 

Scenes

Manage multiple instances of your app’s UI simultaneously, and direct resources to the appropriate instance of your UI.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值