SwiftUI的新应用生命周期以及iOS 14中AppDelegate和SceneDelegate的替代品

During WWDC 2020, SwiftUI got its own app-lifecycle in a bid to get away from UIKit’s AppDelegate and SceneDelegate. To do so, iOS 14 now offers an App Protocol, a SceneBuilder, scenePhase enumerator, and a new property wrapper UIApplicationDelegateAdaptor. Before we look at what they are, let’s quickly brush up SceneDelegate.

在WWDC 2020期间,SwiftUI获得了自己的应用程序生命周期,以摆脱UIKit的AppDelegate和SceneDelegate。 为此,iOS 14现在提供了一个App协议,一个SceneBuilderscenePhase枚举器和一个新的属性包装器UIApplicationDelegateAdaptor 。 在查看它们是什么之前,让我们快速刷一下SceneDelegate

SceneDelegate was introduced in iOS 13 primarily to address multi-window support on iPadOS. It brought a transition from the concept of windows to scenes and allowed us to transfer responsibilities from the AppDelegate.

在iOS 13中引入SceneDelegate的主要目的是解决iPadOS上的多窗口支持问题。 它带来了从窗口概念到场景的转变,并允许我们从AppDelegate转移职责。

Starting iOS 14, when you create a new SwiftUI Application in an Xcode 12 or above project, you’d be given an option to choose between SwiftUI App Lifecycle and UIKit App Delegate. While the latter would generate the same old AppDelegate and SceneDelegate boilerplate code with UIHostingController used for embedding SwiftUI Views, the former would greet you with a new starting point, purely for SwiftUI.

从iOS 14开始,当您在Xcode 12或更高版本的项目中创建新的SwiftUI应用程序时,系统会为您提供一个选项,供您选择SwiftUI App LifecycleUIKit App Delegate 。 后者将使用用于嵌入SwiftUI视图的UIHostingController生成相同的旧AppDelegateSceneDelegate样板代码,而前者将纯粹为SwiftUI迎接新的起点。

SwiftUI应用协议:新起点 (SwiftUI App Protocol: A New Starting Point)

@main
struct ProjectName: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

While the above piece of code is short, there’s a lot of automatic stuff happening under the hood. Primarily, the struct gives a birds-eye view of a SwiftUI application’s hierarchy by unifying Scenes andViews together in one place. A few important things to take note from the code are:

尽管上面的代码很短,但是在后台却发生了很多自动的事情。 首先,该结构通过将ScenesViews统一在一个位置来提供SwiftUI应用程序层次结构的鸟瞰图。 代码中需要注意的一些重要事项是:

  • The @main attribute was introduced with Swift 5.3 and indicates that the above struct is the starting point of our SwiftUI application.

    @main属性是Swift 5.3引入的,它指示上述结构是SwiftUI应用程序的起点。

  • By conforming to the App protocol, we’re required to implement a SceneBuilder which is essentially a function builder for composing one or more scenes. Since we’re using only a single scene in the above example, we’ve implemented the computed property body. Also, the App protocol is responsible for triggering the main() method that launches a SwiftUI application.

    通过遵守App协议,我们需要实现一个SceneBuilder ,它本质上是一个用于构建一个或多个场景的函数构建器。 由于在上面的示例中仅使用单个场景,因此我们实现了calculated属性body 。 同样, App协议负责触发main()方法,该方法启动SwiftUI应用程序。

  • WindowGroup in the above example is a container scene that wraps our SwiftUI views. Running the above application on an iPadOS or macOS can create multiple WindowGroup scenes as they support those features.

    WindowGroup中的WindowGroup是包装我们SwiftUI视图的容器场景。 在iPadOS或macOS上运行上述应用程序可以创建多个WindowGroup场景,因为它们支持这些功能。

Besides WindowGroup, you can also use a DocumentGroup scene type for document-based apps or a Settings and WKNotificationScene for a macOS or watchOS.

除了WindowGroup之外,您还可以将DocumentGroup场景类型用于基于文档的应用程序,或者将SettingsWKNotificationScene用于macOS或watchOS。

You can also set commands modifier on the WindowGroup scene to add key shortcuts to the scene. In order to do so, we need to leverage the new CommandsBuilder to group CommandMenu that uses Commands Protocol.

您还可以在WindowGroup场景中设置命令修饰符,以将关键快捷方式添加到场景。 为此,我们需要利用新的CommandsBuilder对使用Commands Protocol的CommandMenu进行分组。

Setting the computed property to @SceneBuilder is essential when you’re composing complex scenes. For instance, the one given below creates a Preference Menu scene for the macOS platform.

合成复杂场景时,必须将计算属性设置为@SceneBuilder 。 例如,下面给出的将为macOS平台创建一个“首选项菜单”场景。

SwiftUI的新应用生命周期以及iOS 14中AppDelegate和SceneDelegate的替代品_第1张图片

如何收听SceneDelegate生命周期方法? (How to Listen to SceneDelegate Lifecycle Methods?)

Despite the reduced boilerplate code, one could wonder how to listen to lifecycle updates of a scene — as we do in SceneDelegate. Gladly, we have a scenePhase enumerator that holds the current state of the scene. We can use it as an Environment property wrapper and an onChange modifier to listen to state changes of our scene as shown below:

尽管减少了样板代码,但人们可能想知道如何侦听场景的生命周期更新,就像我们在SceneDelegate所做的SceneDelegate 。 很高兴,我们有一个scenePhase枚举器,用于保存场景的当前状态。 我们可以将其用作Environment属性包装器和onChange修饰符,以监听场景的状态变化,如下所示:

如何通过SwiftUI App协议使用AppDelegate (How to Use AppDelegate With SwiftUI App Protocol)

The AppDelegate class forms a crucial part of our applications. Whether it’s for handling notifications or configuring Firebase, it plays a key role with its different lifecycle methods.

AppDelegate类构成了我们应用程序的关键部分。 无论是用于处理通知还是配置Firebase,它都以其不同的生命周期方法发挥关键作用。

In order to hook the AppDelegate functionality, we’d need to bring back UIKit (in order to conform to UIApplicationDelegate) into what was a pure SwiftUI application until now. SwiftUI provides a new property wrapper UIApplicationDelegateAdaptor through which we can inject the AppDelegate instance in our SwiftUI structure:

为了挂钩AppDelegate功能, AppDelegate ,我们需要将UIKit(为了符合UIApplicationDelegate )带回到纯SwiftUI应用程序中。 SwiftUI提供了一个新的属性包装器UIApplicationDelegateAdaptor通过它我们可以将AppDelegate实例注入到我们的SwiftUI结构中:

Likewise, we can use the WKExtensionDelegateAdaptor property wrapper to provide the delegate from watchOS.

同样,我们可以使用WKExtensionDelegateAdaptor属性包装器来提供watchOS的委托。

结论 (Conclusion)

We saw a new property wrapper for injecting AppDelegate into our SwiftUI structure and a new function builder — SceneBuilder — for composing scenes.

我们看到了一个新的属性包装器,用于将AppDelegate注入到我们的SwiftUI结构中,以及一个新的函数构建器SceneBuilder用于构成场景。

The new SwiftUI App lifecycle helps kickstart SwiftUI development very quickly. You can leverage the new @SceneStorage property wrapper for state restoration across multiple scenes of your apps.

新的SwiftUI App生命周期有助于快速启动SwiftUI开发。 您可以利用新的@SceneStorage属性包装器在应用程序的多个场景之间进行状态恢复。

Remember SceneDelegates are neither deprecated in iOS 14 nor in the latest SwiftUI iteration. Apple has only brought a new App-Lifecycle for building native SwiftUI apps that doesn’t depend on UIKit.

请记住,iOS 14和最新的SwiftUI迭代均不建议使用SceneDelegates。 苹果仅带来了一个新的App-Lifecycle,用于构建不依赖UIKit的本机SwiftUI应用。

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/better-programming/swiftuis-new-app-lifecycle-and-replacements-for-appdelegate-and-scenedelegate-in-ios-14-c9cf4a2367a9

你可能感兴趣的:(ios)