简体   繁体   中英

Firebase exception error for macOS app Xcode

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.

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.

I solved it. Instead of using the AppDelegate I used the App's initializer:

struct testagainApp: App {
  init() {
    FirebaseApp.configure()
  }
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM