Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch a tutorial on first app launch using SwiftUI?

Tags:

swiftui

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
}
like image 863
Austin Robinson Avatar asked Jun 20 '26 22:06

Austin Robinson


1 Answers

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")
like image 169
FradSer Avatar answered Jun 24 '26 07:06

FradSer