I would like to launch a tutorial when my SwiftUI app first launches. Where in the project should this code go and how do I launch the tutorial, which is just a SwiftUI View, when the app first launches?
I already know how to check if the app has launched before using UserDefaults. I am wanting to know how to launch the SwiftUI view and then how to launch the standard ContentView after the user completes the tutorial.
let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
if hasLaunchedBefore {
// Not first launch
// Load ContentView here
} else {
// Is first launch
// Load tutorial SwiftUI view here
UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") // Set hasLaunchedBefore key to true
}
In your SampleApp which after SwiftUI 2.0:
import SwiftUI
@main
struct SampleApp: App {
@AppStorage("didLaunchBefore") var didLaunchBefore: Bool = true
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
if didLaunchBefore {
SplashView()
} else {
ContentView()
}
}
}
}
Then add a button with action like below in SplashView:
UserDefaults.standard.set(false, forKey: "didLaunchBefore")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With