Post

Replies

Boosts

Views

Activity

Handling Multi-Sheet Presentation in SwiftUI for a Global Login System
Hi everyone, I'm currently working on a SwiftUI project where I want to create a universal login system that can be invoked from any page within the app. The goal is to ensure that the login page can be presented at any time, such as when a user action or an API response indicates that the user is not authorized. Here's the basic setup I've implemented so far: I've created a LoginManager class to manage the login state globally: @MainActor public class LoginManager: ObservableObject, TokenProviderProtocol { public static let shared = LoginManager() @Published public var isShowingLogin = false @Published public var isShowingLogout = false } In the main view of the app, I'm observing the LoginManager and presenting the LoginPage or LogoutPage based on the state: @EnvironmentObject var loginManager: LoginManager .sheet(isPresented: $loginManager.isShowingLogin) { LoginPage() } .sheet(isPresented: $loginManager.isShowingLogout) { LogoutPage() } At the topmost view, I'm injecting the LoginManager into the environment: .environmentObject(LoginManager.shared) The Issue: The issue I'm facing occurs when attempting to present multiple sheets. Specifically, if I present a sheet on the home page or navigate to another page using a NavigationLink and then try to show the login page, it doesn't display as expected. This problem seems to arise due to conflicts between multiple sheet presentations. My Question: How can I solve this multi-sheet presentation issue? I need a way to ensure that the login page can be shown from any page at any time, such as when an API response indicates that the user is unauthorized. What is the best approach to manage this scenario in SwiftUI? Any advice or patterns that can help resolve this issue would be greatly appreciated. Thank you!
1
0
263
Aug ’24