【问题标题】:"The default Firebase app has not yet been configured" when it has - Swift“尚未配置默认的 Firebase 应用程序” - Swift
【发布时间】:2018-12-01 12:14:17
【问题描述】:

我首先让我的 Firebase 身份验证代码工作,一旦我添加了 Firestore 添加数据功能,一切都崩溃了。这是我的错误信息:

> 2018-06-22 00:31:29.238585-0400 Student Council App[4808:1902875]
> [Accessibility] ****************** Loading GAX Client Bundle
> ****************
>         2018-06-22 00:31:29.324018-0400 Student Council App[4808:1902933] 4.11.0 - [Firebase/Core][I-COR000003] The default
> Firebase app has not yet been configured. Add `[FIRApp configure];`
> (`FirebaseApp.configure()` in Swift) to your application
> initialization. Read more: --a link--.
>         2018-06-22 00:31:29.325455-0400 Student Council App[4808:1902875] *** Terminating app due to uncaught exception
> 'FIRAppNotConfiguredException', reason: 'Failed to get FirebaseApp
> instance. Please call FirebaseApp.configure() before using Firestore'
>         *** First throw call stack:
>         (0x18503ad8c 0x1841f45ec 0x104d4aefc 0x104c93328 0x104c935c4 0x18f27dedc 0x18f3df628 0x18f3df360 0x18f27db84 0x18f3df628
> 0x18f3df7a0 0x18f3df360 0x18f27ced4 0x18f589d88 0x18efebfd8
> 0x18ec09254 0x18ebd7550 0x18f207a0c 0x18ebd6e4c 0x18ebd6ce8
> 0x18ebd5b78 0x18f86b72c 0x18ebd5268 0x18f6509b8 0x18f79eae8
> 0x18ebd4c88 0x18ebd4624 0x18ebd165c 0x18ebd13ac 0x187838470
> 0x187840d6c 0x10657d220 0x106589850 0x18786c878 0x18786c51c
> 0x18786cab8 0x184fe3404 0x184fe2c2c 0x184fe079c 0x184f00da8
> 0x186ee3020 0x18eee178c 0x104c944c4 0x184991fc0)
>         libc++abi.dylib: terminating with uncaught exception of type NSException

尽管如此,我显然已经在我的 AppDelegate 中进行了配置:

import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
}

这是我的视图控制器:

    import UIKit
        import Firebase
        import FirebaseAuth
        class ViewController: UIViewController, UITextFieldDelegate {
            @IBOutlet weak var passwordTextField: UITextField!
            @IBOutlet weak var emailTextField: UITextField!
        let db = Firestore.firestore()
        let userID = Auth.auth().currentUser!.uid
        override func viewDidLoad() {
            super.viewDidLoad()
            self.passwordTextField.delegate = self
            // Do any additional setup after loading the view, typically from a nib.
        }
        override func viewDidAppear(_ animated: Bool) {
            if Auth.auth().currentUser != nil {
                self.presentLoggedInScreen()
            }
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.view.endEditing(true)
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            passwordTextField.resignFirstResponder()
            return (true)
        }
        @IBAction func logInTapped(_ sender: Any) {
            if let email = emailTextField.text, let password = passwordTextField.text {
                Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
                    if let firebaseError = error {
                        print(firebaseError.localizedDescription)
                        return
                    }
                    self.presentLoggedInScreen()
                })
            }
        }
        @IBAction func createAccountTapped(_ sender: Any) {
            if let email = emailTextField.text, let password = passwordTextField.text {

                Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
                    if let firebaseError = error {
                        print(firebaseError.localizedDescription)
                        return
                    }
self.db.collection("users").document(self.userID).setData([ "email": email ])
                    self.presentLoggedInScreen()
                })
            }
        }
        func presentLoggedInScreen() {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let loggedInVc:LoggedInVc = storyboard.instantiateViewController(withIdentifier: "LoggedInVc") as! LoggedInVc
            self.present(loggedInVc, animated: true, completion: nil)
        }
    }

我显然对 Swift 和一般应用程序开发很陌生,这可能真的很愚蠢。提前致谢!

【问题讨论】:

    标签: swift firebase google-cloud-firestore


    【解决方案1】:

    尝试在 viewDidLoad 方法中调用 let db = Firestore.firestore()let userID = Auth.auth().currentUser!.uid

    另一种解决方案是编写:

    lazy var db = Firestore.firestore()
    lazy var userID = Auth.auth().currentUser!.uid
    

    【讨论】:

    • 谢谢!这太荒谬了,它导致了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2016-09-16
    • 2018-01-15
    • 2021-09-22
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多