Вы должны следовать Combine Framework (Reactive Programming)
Создать наблюдаемый объект
class UserAuth: ObservableObject {
@Published var userLoggedIn: Bool = false
}
Там, где вы создаете свой класс делегата сцены, добавьте свой объект среды
func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let userAuth = UserAuth()
let contentView = AppNavigationView().environmentObject(userAuth)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
обновить тот же объект среды в вашем классе входа, что и ниже
struct LoginView: View {
@EnvironmentObject var userAuth: UserAuth
func callWebservicePerformLogin() {
//update the user auth object which will send updates t
self.userAuth.userLoggedIn = true
}
}
Класс слушателя
struct AppNavigationView: View {
@EnvironmentObject var userAuth: UserAuth
var body: some View {
if !userAuth.userLoggedIn {
return AnyView(LoginView())
} else {
return AnyView(PlayerList())
}
}
}