Feel free to edit my title for better clarity.
I am starting a new iOS project and am no longer using SceneDelegate/AppDelegate. My problem is I want my ObservableObject to be an Environmental Object for my entire project but am having trouble converting and finding recent examples.
This is how I defined it in my previous iOS 13 project.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//Environmental View
let observer = GlobalObserver()
let baseView = SplashScreenView().environment(\.managedObjectContext, context)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: baseView.environmentObject(observer))
self.window = window
window.makeKeyAndVisible()
}
}
Here is my main simplified
@main
struct DefaultApp: App {
//Environmental View
let observer: GlobalObserver
init(){
observer = GlobalObserver()
}
var body: some Scene {
WindowGroup {
LoginView()
//.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
The project generated a PersistenceController which I presume had to do with local storage. Do I need to somehow pass my observer into the .environment for loginView?