"The default Firebase app has not yet been configured" 当它有 - 斯威夫特



我首先让我的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

尽管如此,我显然已经在我的应用程序代表中进行了配置:

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 和应用程序开发的新手,这可能是一件非常愚蠢的事情。 提前感谢!

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

另一种解决方案是编写:

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

相关内容

最新更新