AppDelegate的初介绍 Day7

今天打了疫苗,实在太不舒服了

只好来变成分享今天查到的关於AppDelegate整理


AppDelegate:

  • 设置好第一个VC,也就是RootViewController
  • 设置好App相关的设定跟开始的组件
  • 注册Push Notification
  • 回应App的生命周期事件,像是进入Background,回到App等...

先来介绍AppDelegate的生命周期事件与SceneDelegate的相关

AppDelegate.swift

// iOS 13以前的AppDelegate
// 现在没有

func applicationWillResignActive(_ application: UIApplication) {
    //即将变成非活动状态
		// WillResignActive 将会取消活动
}
func applicationDidEnterBackground(_ application: UIApplication) {
    //已进入後台
}
func applicationWillEnterForeground(_ application: UIApplication) {
    //即将进入前台
}
func applicationDidBecomeActive(_ application: UIApplication) {
    //已进入前台,成为活跃进程
}
func applicationWillTerminate(_ application: UIApplication) {
    //进程终止
}
// 现在还有,原本上面的部分被SceneDelegate取代

// 初始化数据库,启动必要服务,注册通知工作,创建UIWindow

// 执行UIWindow的MakeKeyAndVisible方法,将页面显示萤幕上

// AppDelegate中,didFinishLaunchingWithOptions仅负责处理除了UI之外的初始化工作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
// 现在iOS 13 之後新增的 在AppDelegate内用来管例SceneDelegate的

// 新建场景,返回场景配置
//系统将调用configurationForConnecting接口,返回UISceneConfiguration对象,创建scene。
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
// 现在iOS 13 之後新增的 在AppDelegate内用来管例SceneDelegate的

//新建场景,返回场景配置
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

//场景关闭时调用
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}

UIApplicationDelegate不再负责UI的生命周期,负责仅是APP的生命周期,包括启动和中止。


SceneDelegate.swift

// 新建窗口时调用
// 通过app switcher关闭该窗口时调用,至此窗口生命结束。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions){
		guard let _ = (scene as? UIWindowScene) else { return }
    }
func sceneDidDisconnect(_ scene: UIScene){}
func sceneDidBecomeActive(_ scene: UIScene){}
func sceneWillResignActive(_ scene: UIScene){}
func sceneWillEnterForeground(_ scene: UIScene){}
func sceneDidEnterBackground(_ scene: UIScene){}

// WillConnectTo & sceneDidDisconnect以控制场景的新建和关闭。

UISceneSession是管理scene的实例,一个scene对应一个UISceneSession

UISceneSession包含唯一标示和场景的配置细节。


之後的内容会在做更多的补充,但今天就先这样


参考连结:

iOS13SceneDelegate详解

Scene Delegate vs. App Delegate Explained

IOS13 SceneDelegate详解

关于iOS 13 SceneDelegate 适配

iOS13SceneDelegate详解

https://easeapi.com/blog/blog/97-ios13-scene-delegate.html


<<:  Day 6 Compose UI Shape + Composable

>>:  [自然语言处理基础] Regular Expression (II): 文本清理

[Day 3] 一同面对焦虑吧!

焦虑感的来袭 不得不说,在有工作的情形下,一方面要努力完成现有的工作,另一方面还得焦虑自己是否能够真...

EP 19: Custom App Icon for Android and iOS

Hello, 各位 iT邦帮忙 的粉丝们大家好~~~ 本篇是 Re: 从零开始用 Xamarin 技...

[NestJS 带你飞!] DAY23 - Authentication (上)

相信各位在使用各大网站提供的功能时,都会需要注册帐号来获得更多的使用体验,比如:google、fac...

离职倒数19天:霸道总裁到底是不存在还是我没遇到而已

七夕时,凑巧跟大家一起读了佛洛姆的《爱的艺术》这本老书。看完这本书,我觉得我至少确定了一件怀疑很久的...

Day26 如何改善专注力?

大家好,我是乌木白,今天想和大家谈谈专注力这件事情。 浅谈自己的专注力 认识我的人,都知道其实我的...