4

I have a problem right now: What I want: The first time the app starts my already existing view should be presented. I already implemented something in the AppDelegate that checks if the app launched for the first time. And if thats the case another view should be presented. Is there a method to do this directly in the AppDelegate like it was possible with Storyboards?

Thank you in advance.

2 Answers 2

1

In your AppDelegate you have a hosting controller that bootstraps the main SwiftUI view. So one way to achieve this is to conditionally set the rootView.

UIHostingController(rootView: isFirstTime ? FirstTimeView() : ContentView())
Sign up to request clarification or add additional context in comments.

Comments

1

I would create an initial RootView that merely switches between content and provides an EnvironmentValues that's passed to it.

struct RootView: View {
  @Environment(\.isInitialLaunch) var isInitialLaunch: Bool

  var body: some View {
    Group {
      if isInitialLaunch {
        FirstTimeView()
      } else {
        ContentView()
      }
  }
}

Then, in SceneDelegate:

self.window?.rootViewController = UIHostingController(rootView: RootView().environment(\.isInitialLaunch, isInitialLaunch))

Or, make isInitialLaunch a @State (or @Binding, @ObservedObject, etc.) variable. This way, after your onboarding process, if you change it to false, SwiftUI will actually automatically animate users to the ContentView.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.