ios13以上SceneDelegate介绍及swiftUI应用

2023-10-30 17:31

本文主要是介绍ios13以上SceneDelegate介绍及swiftUI应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

   背景:自从Xcode11发布以来,当使用新Xcode创建一个新的ios项目时,sceneDelegate会被默认创建

那么,这个多出的类是做什么的呢?

简单来说,是ios13之后AppDelegate这个类的职责发生了改变!
在ios之前,Appdelegate的职责全权处理App生命周期和UI生命周期,如图:

而在ios13之后,Appdelegate的职责发生了相应的改变!

   1、处理 App 生命周期2、新的 Scene Session 生命周期

而UI的生命周期就交给新增的SceneDelegate来处理,如图!在这里插入图片描述
在这里插入图片描述

这样的话,我们在Xcode11新建iOS项目时,如果你跟往常一样在Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中创建根控制器,就会崩溃报错!那解决办法有2个!
1,把原先在didFinishLaunchingWithOptions写代码移到SceneDelegate的willConnectTo方法中,如图:
在这里插入图片描述
2,如果您所用到的应用程序并不需要这个功能,可以相应的删除以下代码,这样就跟xcode11之前的版本一样啦~
在这里插入图片描述
在这里插入图片描述
但就目前看来,适配SceneDelegate还是很有必要的,比如用到的swiftUI,小组件等功能,都用到了SceneDelegate的类,所以我们还是未雨绸缪的先了解一下~
我们先看一下在appdelegate做了什么

//会返回一个创建场景时需要的UISceneConfiguration对象func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {print("__7__configurationForConnecting")// 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)}//当用户通过“应用切换器”关闭一个或多个场景时会被调用func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {print("__8__didDiscardSceneSessions")// 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.}

在以下图片中,当需要设置多场景的时候,mutiple windows属性应设置为Yes,在application session role中的configuration name应与代码中UIsceneConfiguration的配置名字相同,而delegate class name应与所在的类名相同!
在这里插入图片描述
好啦~再来看一下sceneDelegate中的代码

//当场景添加到app中时,函数会被调用,这里是配置场景的最理想地方,相当于application的didFinishLaunchingWithOptions!(一个delegate配置所有场景)func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {print("__1__willConnectTo")// 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 }// Create the SwiftUI view that provides the window contents.let contentView = ContentView()
//    // Use a UIHostingController as window root view controller.if let windowScene = scene as? UIWindowScene {let window = UIWindow(windowScene: windowScene)window.rootViewController = UIHostingController(rootView: contentView)self.window = windowwindow.makeKeyAndVisible()}}//当场景与app断开连接是调用(注意,以后它可能被重新连接)func sceneDidDisconnect(_ scene: UIScene) {print("__2__sceneDidDisconnect")// 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).}//当用户开始与场景进行交互(例如从应用切换器中选择场景)时,会调用func sceneDidBecomeActive(_ scene: UIScene) {print("__3__sceneDidBecomeActive")// Called when the scene has moved from an inactive state to an active state.// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.}//当用户停止与场景交互(例如通过切换器切换到另一个场景)时调用func sceneWillResignActive(_ scene: UIScene) {print("__4__sceneWillResignActive")// Called when the scene will move from an active state to an inactive state.// This may occur due to temporary interruptions (ex. an incoming phone call).}//当场景变成活动窗口时调用,即从后台状态变成开始或恢复状态func sceneWillEnterForeground(_ scene: UIScene) {print("__5__sceneWillEnterForeground")// Called as the scene transitions from the background to the foreground.// Use this method to undo the changes made on entering the background.}//当场景进入后台时调用,即该应用已最小化但仍存活在后台中func sceneDidEnterBackground(_ scene: UIScene) {print("__6__sceneDidEnterBackground")// Called as the scene transitions from the foreground to the background.// Use this method to save data, release shared resources, and store enough scene-specific state information// to restore the scene back to its current state.}

注:如果适配了SceneDelegate,在ios13以下的application的判断app状态的方法失效

官网支持多场景:
https://developer.apple.com/documentation/uikit/uiscenedelegate/supporting_multiple_windows_on_ipad

参考文章:
1,https://blog.csdn.net/potato512/article/details/106542809
2,https://www.jianshu.com/p/6d6573fbd60b

这篇关于ios13以上SceneDelegate介绍及swiftUI应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/309506

相关文章

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使

Python循环缓冲区的应用详解

《Python循环缓冲区的应用详解》循环缓冲区是一个线性缓冲区,逻辑上被视为一个循环的结构,本文主要为大家介绍了Python中循环缓冲区的相关应用,有兴趣的小伙伴可以了解一下... 目录什么是循环缓冲区循环缓冲区的结构python中的循环缓冲区实现运行循环缓冲区循环缓冲区的优势应用案例Python中的实现库

SpringBoot整合MybatisPlus的基本应用指南

《SpringBoot整合MybatisPlus的基本应用指南》MyBatis-Plus,简称MP,是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,下面小编就来和大家介绍一下... 目录一、MyBATisPlus简介二、SpringBoot整合MybatisPlus1、创建数据库和

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程