I've been studying SwiftUI and understanding Apps, Scenes and Views from this WWDC20 video. I'm trying to understand these concepts with a sample app which displays a list of movies and helps in booking a movie.
The iOS and iPadOS SwiftUI project has the following App:
@main
struct HelloIOSSwiftUIApp: App {
// Attach the app delegate to the swiftUI app
@UIApplicationDelegateAdaptor
var appDelegate: AppDelegate
var body: some Scene {
MovieListScene()
}
}
From the above code, it can be inferred that the HelloIOSSwiftUIApp has a single scene called MovieListScene. So, to add a new scene to the app,
@main
struct HelloIOSSwiftUIApp: App {
// Attach the app delegate to the swiftUI app
@UIApplicationDelegateAdaptor
var appDelegate: AppDelegate
var body: some Scene {
MovieListScene()
BookMovieScene()
.handlesExternalEvents(matching: ["book movie"])
}
}
Now that two scenes are declared, SwiftUI launches the app and displays the content of the first scene.
In both the above examples, the scenes of the app are declared at compile time. So, how can a new scene be added to/removed from the app dynamically? I want to read some data from a config file and decide whether a particular scene should be displayed or not. How can I achieve this behaviour? Or should the app declare all possible scenes at compile time itself (as shown above)?
I'm aware that SwiftUI views can be updated at runtime by using @State
, @StateObject
, @ObservedObject
and @Published
property wrappers. SwiftUI views are functions of their state... so updating the state can change the views. How can I do the same for scenes?