WWDC Notes

App essentials in SwiftUI

Show Apple's description.
Thanks to the new App protocol, SwiftUI now supports building entire apps! See how Apps, Scenes, and Views fit together. Learn how easy it is to implement the features people expect from a best-in-class product while saving time and reducing complexity. Easily add expected functionality to your interface using the new commands modifier, and explore the ins and outs of the new WindowGroup API. To get the most out of this session, you should have some experience with SwiftUI. Watch “Introduction to SwiftUI” for a primer. Want more SwiftUI? Take your pick: “What’s new in SwiftUI”, “Data essentials in Swift UI ”, "Stacks, grids, and outlines in SwiftUI", and “Build document-based apps in SwiftUI”.

Views, Scenes, and Apps

  • Beside Views, from this year SwiftUI can declare Scenes and Apps
  • Views are the basic building blocks, rendering everything on screen. Views can be composed with other Views.
  • Views form the content of a Scene, making them platform-independent
  • Scenes can also be composed with other Scenes (think of tabs on macOS)
  • Scenes form the content of an App

Example

@main
struct MyApp: App {
    @StateObject private var store = Store()

    var body: some Scene {
        WindowGroup {
            MyView(store: store)
        }
    }
}
  • In thie example we have an App, MyApp, which contains a Scene, WindowGroup, which contains a view MyView.
  • Above the struct declaration we have a new Swift 5.3 attribute @main, which declares this struct as the entry point of our programs execution.
  • Declaring Apps is very similar to declaring Views: both comform to a protocol (App vs View), and both need to define a body (of type some Scene vs some View).
  • Like Views, also Apps can declare data depenencies, and in this example MyApp declares to be owner of the store object by using the StateObject property wrapper,

WindowGroup

  • Scene that allows us to define the primary interface of our app.
  • WindowGroup is a scene that allows our app to automatically create multiple scenes containing MyView when needed (this works for having multple windows on both iPad and mac).
  • Like in UIKit, an app can provide a shared model for each scene to use, but the state of the views in those scenes will be independent.
  • WindowGroup also manages tabs for us on macOS.
  • @SceneStorage is a new property wrapper that manages scenes restoration.

More New features

  • DocumentGroup: New scene that help us with document based apps, more on this in Build Document-Based Apps in SwiftUI session.
  • Settings is a macOS-only scene used for declaring apps preferences
  • The Commands API (along with the .commands modifier) let us describe commands to be shown in the main menu on macOS and key commands on iOS.

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

zntfdr

Federico Zanetello

Federico Zanetello is an iOS Engineer with strong passion for Swift, minimalism, and design. When he’s not busy automating things, he can be found writing at fivestars.blog and/or playing with the latest shiny toys.