app启动之后就进来这里了啊,或者说启动也是在这个的范畴啊,【任何生命状态的转变,UIKit都会通知委托对象XXXDelegate】
1、As your app changes from state to state, you must adjust its behavior accordingly. 状态决定行为
In iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app. #基于场景
In iOS 12 and earlier, use the UIApplicationDelegate
object to respond to life-cycle events. #基于app
-- app有foreground、 background、unattended 三种大状态,然后还有活跃与非活跃状态,详细看文档,我还没看完啊。
而你应该在每种状态中定义响应的行为,是什么人就做什么事
-- iOS 13 及之后都是用
UISceneDelegate
这个对象来代表你的app的生命周期,所以 iOS 12 及以前就是用
UIApplicationDelegate
了咯
-- 所以 iOS 13 是基于 场景(scene) 的生命周期事件 Respond to Scene-Based Life-Cycle Events
而 iOS12 则是基于app的生命周期事件 Respond to App-Based Life-Cycle Events
苹果更新快,所以代码的迭代周期也快啊,可能ios14又不一样了啊
--The user can create multiple scenes for each app, and show and hide them separately
你可以为一个app创建多个scene,每一个scene都有自己的生命周期,是一个独立生命体,象征一个UI
--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.scene是一个可选特征,你需要在app’s
Info.plist文本中选择使用scene机制
生命周期:
启动阶段 launch time ,是app准备时期,此时的生命状态为unattached
㊀ --At launch time, the system starts your app in the inactive state before transitioning it to the foreground.
在启动阶段 进入的inactive状态:在进入foreground状态之前,iOS会将你的app置于inactive状态,亦即是foreground的闲置状态,启动阶段的inactive状态下,UIKit会调用 委托对象 的这两个方法:
application(_:willFinishLaunchingWithOptions:)和
application(_:didFinishLaunchingWithOptions:)
所以你可以在这两个方法中设计相应的操作(app首次启动)。
㊁ --For an app that is in the background, UIKit moves your app to the inactive state by calling one of the following methods:
For apps that support scenes—The
sceneWillEnterForeground(_:)
method of the appropriate scene delegate object.For all other apps—The
applicationWillEnterForeground(_:)
method.在状态转换阶段 进入的inactive状态: 从background转换到foreground时,iOS首先也会先进入foreground中的inactive状态,此时UIKit会自动调用
sceneWillEnterForeground(_:)
或applicationWillEnterForeground(_:)
方法,因而你可以在这两个方法中进行你的表演。
㊂ --The system moves your app to the active state immediately before displaying the app’s UI
从inactive转换到active状态:在显示你的界面之前,iOS会立马从inactive转换到active状态,也就是说inactive已经准备就绪了显示需要的所有资源。
在active状态下,你可以进行以下的操作:
⊙Activation is a good time to configure your app’s UI and runtime behavior; specifically:
--Show your app’s windows, if needed. //按需展示你的window
--Change the currently visible view controller, if needed. //改变view的可视性
--Update the data values and state of views and controls. //更新view的状态值,更新控制行为
--Display controls to resume a paused game. //展示控制,恢复之前被暂停的游戏
-- Start or resume any dispatch queues that you use to execute tasks. //开始或者恢复你的分发队列,以便执行任务
--Update data source objects. //更新数据源对象
--Start timers for periodic tasks. //开始周期性任务
以上的操作的代码在下面这两个 活跃状态的方法 中写:
Put your configuration code in one of the following methods:
• For a scene-based UI—The
sceneDidBecomeActive(_:)
method of the appropriate scene delegate object.• For all other apps—The
applicationDidBecomeActive(_:)
method of your app delegate object.建议: 应该在background时获取需要网络更新的数据,而不要在foreground中执行获取的代码,或者使用异步队列获取
㊃ --When your activation method returns, UIKit shows any windows that you made visible.
当上面两个 活动方法(activation) 执行return 后,UIKit会展示所有 你设置为可见的 窗体。同时。UIKit会通知这些窗体中的view controllers ,跟它们说 ,你的view可以展示了。
Use your view controller’s
viewWillAppear(_:)
method to perform any final updates to your interface. 使用这个方法去执行 view展示所需要的 最后的琐事。
app的生命状态:
1、When the user or system requests a new scene for your app, UIKit creates it and puts it in the unattached state. ~未连接状态
-- 当你或系统申请一个scene时,UIKit会为你创建,并且首先把这个scene置于unattended状态(通常只是一瞬间,也可能是一段时间的事),记住一个scene对应一个UI
-- User-requested scenes move quickly to the foreground, where they appear onscreen. ~foreground状态
如果是你请求的scene,那么这个scene经过一段时间处理之后会跑到前台运行,在屏幕上展示它对应的UI
-- A system-requested scene typically moves to the background so that it can process an event。 ~background状态
如果是系统请求的scene,那么它就会在后台运行,处理响应的事件逻辑。
● foreground状态时:
▪️▪️Preparing Your UI to Run in the Foreground(点链接)
-- 配置你的app 即将要在屏幕上展示的东西,即是UI,所以foreground状态就是展示你的UI的状态,例如客户点击了图标,
iOS就进入app的启动周期,并且把该app置于foreground状态。
--All state transitions result in UIKit sending notifications to the appropriate delegate object:
• In iOS 13 and later—A
UISceneDelegate
object.(硬件支持的话,默认这个模式)• In iOS 12 and earlier—The
UIApplicationDelegate
object.--就是说app发生状态转换的话,UIKit就会向这两个对象发送消息,所以你的代码中就可以通过获取这两个对象中的
关于状态的属性 的值来判断此时的app处于什么状态,然后你再决定下一步该做什么。
【支持scene机制时:
-- Specifying the Scenes Your App Supports (连接)
• In iOS 13 and later, users can create multiple copies of your app’s UI and toggle between them in the app switcher.
在iOS 13 及之后,你可以创建UI副本,分屏显示,因为有了scene的支持
1、你需要在app’s
Info.plist
file 配置你的scene类型,也可以动态完善你的scene信息配置方法:Open your Xcode project and select the
Info.plist
file. Click the plus (+) button of the ApplicationScene Manifest entry. 更多配置详情看UISceneConfigurations(连接).
2、In addition to creating the scene object, UIKit creates a window for your scene automatically and installs the initial view controller from your storyboard in it.
除了创建scene对象,UIKit还会默认创建对应的窗体和控制器,你可以在
UIWindowSceneDelegate(链接)
中更换控制器。你也可以通过storyboard的方式指定scene对象的UI。
】
▪️▪️Responding to Memory Warnings(点链接)
Free up memory when asked to do so by the system.UIKit delivers low-memory warnings in the following ways:
It calls the
applicationDidReceiveMemoryWarning(_:)
method of your app delegate.It calls the
didReceiveMemoryWarning()
method of any activeUIViewController
classes.It posts a
didReceiveMemoryWarningNotification
object to any registered observers.It delivers a warning to dispatch queues of type
DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
.如果IOS不能通过终结suspend的app来释放内存,那么iOS会向正在运行的app发送低内存警告,发送到上面的这几个方法里,
所以你的app收到低内存警告后,应该在上面这几个方法里尽可能多且尽可能快地进行释放内存的相关操作。
● background状态时:
▪️▪️Preparing Your UI to Run in the Background(点链接)
--进入后台的原因有多种:
(1) app被suspend前的短暂时间内,先短暂进入后台状态,然后才被suspend
(2) 系统直接使app进入后台状态,或者把一个suspend状态的程序使进入后台状态,这样做时可以使app在后台状态下
有时间执行一些重要的任务(虽然应该尽可能少执行任务)
(3) 在 后台状态 释放 前台状态 时使用的资源
--UIKit notifies only the scene delegate associated with the specific scene that is entering the background.UIKit一个时刻只会通知一种或者一个委托对象,基于app的就是UIApplicationDelegate委托对象,基于scene的就是
UISceneDelegate这个委托对象,而且只是这一个与该scene相关的委托对象。
--Quiet Your App upon Deactivation
与后台相对应的还有 钝化状态 ,这时候只是暂停一下app的活动。例如,当用户退出foregroung而还没进入background时,
系统会先一瞬间钝化app,然后后进入background状态,这是从foreground-->background 的中间deactivation状态,要与从
background-->foreground 的中间状态inactive区别,这两个状态转换时不一样的。
►钝化(Deactivation)的一些原因有:
(1) 用户退出foreground状态时
(2) 系统通知弹出的时候
--During deactivation, UIKit calls one of the following methods of your app:
• For apps that support scenes—The
sceneWillResignActive(_:)
method of the appropriate scene delegate object.• For all other apps—The
applicationWillResignActive(_:)
method of the app delegate object.在app进入deactivation状态时,UIKit首先会调用上面两个方法,因此你可以在这两个方法里面进行响应的代码操作,例如
保存数据,挂起分发队列、运作队列,不再计划新任务等等。
--Upon entering the background, UIKit calls one of the following methods of your app:
• For apps that support scenes—The
sceneDidEnterBackground(_:)
method of the appropriate scene delegate object.• For all other apps—The
applicationDidEnterBackground(_:)
method of the app delegate object.当app进入后台状态时,UIKit会调用上面两个方法,因此,你可以在这两个方法中进行释放内存等资源的操作。
--关于后台的 任务执行:
• Updating Your App with Background App Refresh(点链接)
Fetch content opportunistically in the background and update your app's interface.
• Extending Your App's Background Execution Time(点链接)
Ensure that critical tasks finish when your app moves to the background.
• About the Background Execution Sequence(点链接)Learn the order in which your custom code is executed when your app moves to the background.
2、When the user dismisses your app's UI, UIKit moves the associated scene to the background state and eventually to the suspended state. ~后台和挂起状态
-- 当客户解散app中的一个UI时,UIKit也会将它相关联的scene移到后台状态,没什么事的话,最终这个scene会被置成挂起状态
● suspend状态时:
--Processing Queued Notifications(点链接)
Respond to notifications when coming out of the suspended state. These notifications are delivered immediately to apps that are running, but their delivery is delayed for apps that are suspended.
当app从suspend状态中复活时,亦即是suspend状态的app将要运行时,系统会优先处理这些通知,甚至是 在客户点击屏幕
的事件处理之前,所以优先级很高。这些通知一般是系统的通知或者设置改变的通知。