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

相关文章

利用Python操作Word文档页码的实际应用

《利用Python操作Word文档页码的实际应用》在撰写长篇文档时,经常需要将文档分成多个节,每个节都需要单独的页码,下面:本文主要介绍利用Python操作Word文档页码的相关资料,文中通过代码... 目录需求:文档详情:要求:该程序的功能是:总结需求:一次性处理24个文档的页码。文档详情:1、每个

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

Java 缓存框架 Caffeine 应用场景解析

《Java缓存框架Caffeine应用场景解析》文章介绍Caffeine作为高性能Java本地缓存框架,基于W-TinyLFU算法,支持异步加载、灵活过期策略、内存安全机制及统计监控,重点解析其... 目录一、Caffeine 简介1. 框架概述1.1 Caffeine的核心优势二、Caffeine 基础2

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N