Given the following setup:
Environment Variable UserState
class UserState: ObservableObject {
@Published var loggedIn = Auth.auth().currentUser != nil
}
UserState as a variable in SceneDelegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
//creating the variable
var userState = UserState()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(userState))
}
...
}
I can now perfectly read/write to this variable in SwiftUI views by declaring
struct ProfileTab: View {
@EnvironmentObject var userState: UserState
var body: some View {
// here I could easily read/write to userState
}
}
So far so good. But: What is the proper way to write to this variable outside of a SwiftUI view? E.g. from a class or class extension.
Example
extension AppDelegate {
func something(loggedIn: Bool) {
// here I would like to access the environment variable. Something like
// userState.loggedIn = loggedIn
}
}
Here is possible approach...
class AppDelegate: UIResponder, UIApplicationDelegate {
//creating the variable in AppDelegate to have it shared
var userState = UserState()
...
so, then you can ...
extension AppDelegate {
func something(loggedIn: Bool) {
// just use it here as now it is own property
self.userState.loggedIn = loggedIn
}
}
and use it in scene delegate via shared application instance
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// safe as it is known the one AppDelegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
window.rootViewController = UIHostingController(rootView:
ContentView().environmentObject(appDelegate.userState))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With