SceneDelegate 和 AppDelegate 的区别 [英] Difference between SceneDelegate and AppDelegate

查看:88
本文介绍了SceneDelegate 和 AppDelegate 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 SwiftUI 项目中,我看到了 AppDelegate 文件以及一个 SceneDelegate 文件.

In my SwiftUI project I see AppDelegate file as well as a SceneDelegate file.

它们之间有什么区别?

例如在SceneDelegate中的方法之间

scene(_:willConnectTo:options:)

并在 AppDelegate

application(_:didFinishLaunchingWithOptions:)

推荐答案

这两个文件旨在通过运行整个应用程序所需的内容以及可以明显支持的一个实例"所需的内容来拆分工作在后台运行.这类似于配置一次数据库,但按窗口显示不同的值集.

The two files are meant to split the work by what is needed to run the app as a whole and what is needed for one "instance" that would support visibly running in the background. This would be something like configuring a database once, but displaying different sets of values by window.

您可以将它们视为全局和私有版本.一个是共享的,另一个仅限于个人所有者.在某种程度上,它们正是您所期望的名称.

You could think of them as the global and private versions. One is shared and the other is limited to the individual owner. In a way, they are exactly what you would expect by the names.

多窗口支持正在发生

下次你创建一个新的 Xcode 项目时,你会看到你的 AppDelegate一分为二:AppDelegate.swift 和 SceneDelegate.swift.这是一个iPadOS 带来的新多窗口支持的结果,以及有效地将应用委托的工作一分为二.

Next time you create a new Xcode project you’ll see your AppDelegate has split in two: AppDelegate.swift and SceneDelegate.swift. This is a result of the new multi-window support that landed with iPadOS, and effectively splits the work of the app delegate in two.

从 iOS 13 开始,您的应用委托应该:

From iOS 13 onwards, your app delegate should:

  1. 设置应用运行期间所需的任何数据.
  2. 响应与应用相关的任何事件,例如与您共享的文件.
  3. 注册外部服务,例如推送通知.
  4. 配置您的初始场景.

相比之下,场景代理用于处理您的一个实例应用程序的用户界面.所以,如果用户创建了两个窗口显示您的应用有两个场景,均由同一个应用委托提供支持.

In contrast, scene delegates are there to handle one instance of your app’s user interface. So, if the user has created two windows showing your app, you have two scenes, both backed by the same app delegate.

请记住,这些场景旨在独立于彼此.因此,您的应用程序不再移动到后台,而是个别场景做 - 用户可能会移动一个到背景同时保持另一个开放.

Keep in mind that these scenes are designed to work independently from each other. So, your application no longer moves to the background, but instead individual scenes do – the user might move one to the background while keeping another open.

https://www.hackingwithswift.com/提供文章/193/whats-new-in-ios-13

这篇关于SceneDelegate 和 AppDelegate 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆