默认的 Firebase 应用程序尚未配置,但 FirebaseApp.configure() 添加到了 appdelegate 的 didFinishLaunchingWithOptions 方法

问题描述 投票:0回答:4

我正在尝试在我的 ios 应用程序上配置 firebase 云消息传递。 根据 Firebase 文档的说明,我添加了这样的 Firebase 配置(appdelegate.swift)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { IQKeyboardManager.shared.enable = true

if #available(iOS 10.0, *) {
    UNUserNotificationCenter.current().delegate = self
    
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions,
        completionHandler: { _, _ in }
    )
} else {
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

Messaging.messaging().isAutoInitEnabled = true
Messaging.messaging().delegate = self

Messaging.messaging().token { token, error in
    if let error = error {
        print("Error fetching FCM registration token: \(error)")
    } else if let token = token {
        self.fcmId = token
        print("\n My FCM registration token: \(token) \n")
    }
}

FirebaseApp.configure()

//For check update
Siren.shared.wail()

if UserDefaults.standard.value(forKey: "login") as? String == "yes" {
    
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    let navigationController = UINavigationController(rootViewController: nextViewController)
    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.window!.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    
}else{
    
    let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "goToLogin") as! LoginViewController
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewControlleripad
    self.window?.makeKeyAndVisible()
}
return true

}

当我在 iPhone 6 上运行该应用程序时,我得到了。

[Firebase/Core][I-COR000003] 默认 Firebase 应用尚未配置。将

FirebaseApp.configure()
添加到您的应用程序初始化中。这可以在 App Delegate 的 application(_:didFinishLaunchingWithOptions:)
(or the
@main` 结构体在 SwiftUI 中的初始值设定项中完成。

swift firebase-cloud-messaging appdelegate
4个回答
0
投票

FirebaseApp.configure()
移到顶部,因为在调用任何 Firebase 方法之前应首先调用它。


0
投票

我知道这是一篇旧帖子,但看起来还没有正确的解决方案。

有时,仅将

FirebaseApp.configure()
放在
didFinishLaunchingWithOptions
的顶部并没有帮助。

只需将您的

FirebaseApp.configure()
放入 AppDelegate 的
init()
中即可。

因此您可以确定 Firebase 将首先配置。

class AppDelegate: UIResponder, UIApplicationDelegate {

    override init() {
        FirebaseApp.configure()
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    
}

0
投票

我知道这是一篇旧帖子,但我写这篇文章是因为我也在为此苦苦挣扎。

我在视图中有一个

init()
作为默认视图打开,从
FireBase
获取数据,基本上,它在调用
FireBase
配置函数之前调用了该 init 并导致了该错误。


0
投票

在我的例子中,我使用 GeoFire 作为 CocoaPods 依赖项,而其他 Firebase 库是使用 Swift 包管理器添加的。 全部移至 CocoaPods 解决了该问题。

我希望 GeoFire 能够与 Firebase 库一起使用 Swift 包管理器。我不得不坚持使用 CocoaPods。

© www.soinside.com 2019 - 2024. All rights reserved.