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

相关文章

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

zookeeper端口说明及介绍

《zookeeper端口说明及介绍》:本文主要介绍zookeeper端口说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、zookeeper有三个端口(可以修改)aVNMqvZ二、3个端口的作用三、部署时注意总China编程结一、zookeeper有三个端口(可以

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h