When I try to run the simulator for my macOS app, which is using Firebase, it gives this error: "Thread 1: "The default FirebaseApp instance must be configured before the default Authinstance can be initialized. One way to ensure this is to call FirebaseApp.configure()
in the App Delegate’s application(_:didFinishLaunchingWithOptions:)
(or the @main
struct’s initializer in SwiftUI)." I notice this happened after I created an environment object.
Here is my code:
import SwiftUI
import FirebaseCore
@main
struct testagainApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
let viewModel = AppViewModel()
ContentView()
.environmentObject(viewModel)
}
.windowStyle(HiddenTitleBarWindowStyle())
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
FirebaseApp.configure()
}
}
If I get rid of let viewModel = AppViewModel() and .environmentObject(viewModel), the simulator runs just fine. If I put the app delegate first, the simulator runs but nothing appears. I am new to Swift and am unsure how to fix this.
2
Answers
I solved it. Instead of using the
AppDelegate
I used the App's initializer:Option 1:
I faced same issue a while ago, then noticed that I have not linked the
GoogleService-Info.plist
file to the project, if you see its already added then better remove and add it to the project via Xcode again.Hope that solves your issue.
Option 2:
Make sure you don’t have more two copies of Firebase linked into your app. If so you can remove one. See this doc on the Firebase iOS SDK for more details.