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

相关文章

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

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

从基础到高阶详解Python多态实战应用指南

《从基础到高阶详解Python多态实战应用指南》这篇文章主要从基础到高阶为大家详细介绍Python中多态的相关应用与技巧,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、多态的本质:python的“鸭子类型”哲学二、多态的三大实战场景场景1:数据处理管道——统一处理不同数据格式

Java Stream 的 Collectors.toMap高级应用与最佳实践

《JavaStream的Collectors.toMap高级应用与最佳实践》文章讲解JavaStreamAPI中Collectors.toMap的使用,涵盖基础语法、键冲突处理、自定义Map... 目录一、基础用法回顾二、处理键冲突三、自定义 Map 实现类型四、处理 null 值五、复杂值类型转换六、处理