APP的生命周期和UIscene,UIwindow(二)

= =将以前收集的资料做个总结

从ios13开始,app的生命周期管理由appdelegate分成了appdelegate和scenedelegate两个文件,前者负责app的启动和结束以及对UIscene对象的管理,后者负责APP的状态切换(not running , inactive, active, background,Suspended)以及相关的方法调用。

康康UIscene的官方描述

 为了适应多场景,推出了这个UIscene管理UI的生命周期,每个scene对应一个keywindow,一个scene可以有很多个window,window像是一个展示UI的容器。一个个UI就是一张张照片,window是相框。

         多场景就像这样

appdelegate中对scene的管理方法。

    // MARK: UISceneSession Lifecycle   scene生命周期

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }//启动scene的配置

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards放弃 a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }//关闭和scene的会话

 scenedelegate对UI的管理:

配置window相关信息的入口

UIwindowscene

管理window的类,继承UIScene
A scene that manages one or more windows for your app.

一个应用程序对应一个UIScene实例,相互独立,同时进行,每一个实例有不同的状态,可能一个在前台运行一个在后台运行,拥有独立的Windows和controllers,这些实例统一油UIApplication.shared管理,并且有个代理UISceneDelegate处理整个生命周期和事件。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
        configVC()
    }
    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
        // 当场景被系统释放时调用。
                // 这发生在场景进入后台后不久,或者当它的会话被丢弃时。
                // 释放与该场景关联的任何资源,这些资源可以在下次场景连接时重新创建。
                // 场景可能稍后重新连接,因为它的会话不一定被丢弃(请参阅 `application:didDiscardSceneSessions`)。
    }

基本上看函数名字就知道是干啥的了- -偷个懒不贴图了。

App启动分为pre-main和main两个过程,上一篇文章总结了pre-main的过程,而main过程就是本文提到的一些方法的调用过程,配置启动的基本信息,提一嘴,swift没有main函数的,

 

main被这个类给代替了,这个类就是程序的入口。如果不想要这个类的话,得重建一个文件,命名为main.swift,然后定义一个类继承自AppDelegate

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值