title: Run code during app launch in SwiftUI 2.0
body: <p>In my App struct, I have a small function that checks to see if the user has opened the app before. If not, it shows an onboarding view with a few questions. Right now, I just have a .onAppear attached to both the Onboarding and ContentView to run the function, but when you launch the app, the Onboarding view flashes for a quick second. How can I run the function during launch, so the Onboarding view doesn't flash in for a second?</p>
  <p>Here's my App struct:</p>
  <pre><code>import SwiftUI
  
  @main
  struct TestApp: App {
      @State private var hasOnboarded = false
      
      var body: some Scene {
          WindowGroup {
              if hasOnboarded {
                  ContentView(hasOnboarded: $hasOnboarded)
                      .onAppear(perform: checkOnboarding)
              } else {
                  Onboarding(hasOnboarded: $hasOnboarded)
                      .onAppear(perform: checkOnboarding)
              }
          }
      }
      
      func checkOnboarding() {
          let defaults = UserDefaults.standard
          let onboarded = defaults.bool(forKey: &quot;hasOnboarded&quot;)
          hasOnboarded = onboarded
      }
  }
  </code></pre>