skip to Main Content

I’m quite new to SwiftUI, so I would appreciate your help. I have an application whose data is private and requires a login screen. I want the login screen to be shown every time the app goes to the background, but I need the app to keep the state (i.e, if the user was in the middle of creating a new item and changes to another app, I want all the data he has typed to be kept, I can live with the data being lost if the app is killed but I want to allow to swap momentarily between apps to let the user copy and paste data between apps).

I’ve been googlin for a solution and people is implementing the login screen with a regular view and showing it based on certain state variable, but I find this approach not working well because:

  • Either the state is not kept when the user changes to another app
  • Or if the .fullScreenCover modifier is used, the state is kept but does not work well if you are presenting views with the sheet modifier (basically, the loginscren is rendered behing the sheet, which is still shown to the user when he brings the app back to front).

I’ve read in a post (although it does not show any code example) that the way to go is go have another scene with the loginScreen and switch between scenes, but I’m not able to implement it with SwiftUI.

So far I’ve tried to implement it by having my app to have two WindowGroup, one with the app contents and another with the login screen. Like the following:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup("MainScene") {
            MainView()
        }
        WindowGroup("LoginScene") {
            LoginScreenView()
        }
    }
}

But I’m not finding how to change between scenes.


Please signup or login to give your own answer.
Back To Top
Search