App essentials in SwiftUI
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”.
View
s, Scene
s, and App
s
- Beside
View
s, from this year SwiftUI can declareScene
s andApp
s
View
s are the basic building blocks, rendering everything on screen.View
s can be composed with otherView
s.
View
s form the content of aScene
, making them platform-independent
Scene
s can also be composed with otherScene
s (think of tabs on macOS)
Scene
s form the content of anApp
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 aScene
,WindowGroup
, which contains a viewMyView
.
- Above the struct declaration we have a new Swift 5.3 attribute
@main
, which declares thisstruct
as the entry point of our programs execution.
- Declaring
App
s is very similar to declaringView
s: both comform to a protocol (App
vsView
), and both need to define abody
(of typesome Scene
vssome View
).
- Like
View
s, also Apps can declare data depenencies, and in this exampleMyApp
declares to be owner of thestore
object by using theStateObject
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 containingMyView
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 inBuild Document-Based Apps in SwiftUI
session.
Settings
is a macOS-only scene used for declaring apps preferences