简体   繁体   English

NSInternalInconsistencyException:Firebase 在启动时崩溃 SwiftUI 应用程序

[英]NSInternalInconsistencyException: Firebase crashes SwiftUI app on launch

I am trying to add Firebase auth via Apple Sign-in and have got this error:我正在尝试通过 Apple 登录添加 Firebase 身份验证,但出现此错误:

crashed due to an uncaught exception `NSInternalInconsistencyException`. Reason: 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)

This is how I launch Firebase:这就是我启动 Firebase 的方式:

import Firebase
import SwiftUI

class AppDelegate: NSObject, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    FirebaseApp.configure()
    return true
  }
}

I did not install the Firebase package directly, but instead added this package. which I believe added Firebase as one of its dependency.我没有直接安装 Firebase package,而是添加了这个 package。我相信它添加了 Firebase 作为其依赖项之一。 I have checked other solutions but they all rely on CocoaPods while I'm using the Swift Package Manager.我已经检查了其他解决方案,但当我使用 Swift Package 管理器时,它们都依赖于 CocoaPods。 Not sure how I can resolve this error.不确定如何解决此错误。

App calling AppDelegate :应用调用AppDelegate

import FirebaseService
import SwiftUI

@main
struct SomeApp: App {
  @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

  @State private var authState = AuthState()

  var body: some Scene {
    WindowGroup {
      switch authState.value {
      case .undefined:
        ProgressView()
      case .authenticated:
        ContentView()
      case .notAuthenticated:
        AuthView()
      }
    }
  }
}

This error message indicates that another part of your app tries to access Firebase Auth before Firebase is initialised in your your app delegate.此错误消息表明您的应用程序的另一部分在您的应用程序委托中初始化 Firebase 之前尝试访问 Firebase Auth。

Looking at the code of Rebeloper's package which you're using, we can see that it calls Auth.auth().addStateDidChangeListener in line 16 of AuthListener .查看您正在使用的Rebeloper 的 package的代码,我们可以看到它在 AuthListener 的第 16 行调用Auth.auth().addStateDidChangeListener addStateDidChangeListener 。 Since you create an instance of AuthState (another class from Rebeloper's library), this is executed before Firebase is initialised.由于您创建了一个AuthState实例(Rebeloper 库中的另一个 class),因此它在 Firebase 初始化之前执行。

Instead of using an undocumented third party library, I would recommend implementing this yourself - especially since it's only a few lines of code.与其使用未记录的第三方库,我建议您自己实现它——特别是因为它只有几行代码。

To see how this is done, check out Getting started with Firebase Auth for Apple platforms - Firebase Fundamentals , which not only explains Firebase Auth and how to handle authentication state in detail, but also includes the corresponding source code.具体是如何实现的,请查看Getting started with Firebase Auth for Apple platforms - Firebase Fundamentals ,其中不仅详细解释了 Firebase Auth 以及如何处理 state 身份验证,还包括相应的源代码。

As an added bonus, this contains a beautiful signup/login form:作为额外的奖励,它包含一个漂亮的注册/登录表单:

在 iOS 模拟器中运行登录屏幕的 Xcode 屏幕截图

For reference, here is the source code for registering an authentication state listener in an ObserableObject :作为参考,以下是在 ObserableObject 中注册身份验证ObserableObject侦听器的源代码:

@MainActor
class AuthenticationViewModel: ObservableObject {
  @Published var email = ""
  @Published var password = ""
  @Published var confirmPassword = ""

  @Published var flow: AuthenticationFlow = .login

  @Published var isValid  = false
  @Published var authenticationState: AuthenticationState = .unauthenticated
  @Published var errorMessage = ""
  @Published var user: User?
  @Published var displayName = ""

  init() {
    registerAuthStateHandler()
    // ...
  }

  private var authStateHandler: AuthStateDidChangeListenerHandle?

  func registerAuthStateHandler() {
    if authStateHandler == nil {
      authStateHandler = Auth.auth().addStateDidChangeListener { auth, user in
        self.user = user
        self.authenticationState = user == nil ? .unauthenticated : .authenticated
        self.displayName = user?.email ?? ""
      }
    }
  }
  // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI Firebase 推送通知出现在应用内 - SwiftUI Firebase Push Notifications appear inside the app 将图像发送到 Firebase 存储时应用程序崩溃 - App crashes when sending image to Firebase storage Firebase Flutter 登录苹果崩溃应用程序 - Firebase Flutter Sign In With Apple Crashes App Firebase 使 Flutter 应用程序构建在它自己的库代码上崩溃 - Firebase makes Flutter App build crashes on it's own library code 将 Firebase 分析与 SwiftUI 一起使用 - Using Firebase Analytics with SwiftUI 如果应用程序在没有 inte.net 的情况下崩溃/关闭,Firebase 本地数据会怎样? - What happens to Firebase local data if app crashes/closes without internet? iOS 应用程序在尝试使用 Firebase 身份验证“使用 Apple 登录”时崩溃 - iOS app crashes when trying to "Sign in with Apple" using Firebase Authentication 如何获取在启动时立即崩溃的 React Native Android 应用程序的崩溃日志 - How to get crash logs for a React Native Android app that crashes immediately on launch Expo react-native app with firebase phone authentication works on web, error on ios simulator and crashes with no warning on Android - Expo react-native app with firebase phone authentication works on web, error on ios simulator and crashes with no warning on Android Next.js 应用程序在尝试实现谷歌 firebase 身份验证时崩溃 - Next.js app crashes when attempting to implement google firebase authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM