设置为“默认Firebase应用尚未配置”-Swift

时间:2018-06-22 04:43:27

标签: swift firebase google-cloud-firestore

我首先启用了Firebase Auth代码,一旦添加了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和应用程序开发是陌生的,这可能真的很愚蠢。预先感谢!

1 个答案:

答案 0 :(得分:5)

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

另一种解决方法是写:

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