I am writing an app in SwiftUI and using Firebase as my backend.
I have a working preferences panel, that consists of a modal within the application itself, but am wanting to utilise macOS's consistent preferences window that includes the menu options as well as shortcut that is written about in this article.
This is my App declaration and delegates.
@main
struct SchedulerApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#else
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
@StateObject var authState = AuthState()
@StateObject var model = Model()
var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 800, minHeight: 450)
.environmentObject(model)
.environmentObject(authState)
}
.commands {
SidebarCommands()
}
#if os(macOS)
Settings {
Preferences()
}
#endif
}
}
#if os(macOS)
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
FirebaseApp.configure()
}
}
#else
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
return true
}
}
#endif
You can see that I have both a delegate and a state object for an object that listens to the authentication state and another that holds user data and settings:
class AuthState: ObservableObject {
var handle: AuthStateDidChangeListenerHandle?
@Published var signedIn: Bool? = nil
init() {
listen()
}
func listen() {
Auth.auth().addStateDidChangeListener { [self] (auth, user) in
switch user == nil {
case true: signedIn = false
case false: signedIn = true
}
}
}
}
However, when I run this code, the following error occurs:
The default Firebase app has not yet been configured. Add
[FIRApp configure];
(FirebaseApp.configure()
in Swift) to your application initialization. Read more: (...)
The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call
[FIRApp configure];
(FirebaseApp.configure()
in Swift) in the App Delegate'sapplication:didFinishLaunchingWithOptions:
(application(_:didFinishLaunchingWithOptions:)
in Swift).
I need the app to have a singular source of truth at the initialisation step - but the applicationWillFinishLaunching for some reason is calling after the StateObject call.
My app currently has these StateObject classes declared in ContentView, which dodges these errors, but as I mentioned above, I need preferences to share a single source of truth.
Any help is much appreciated!