无法获取默认的Firebase数据库实例。必须调用`[FIRApp配置]

时间:2017-09-04 21:00:35

标签: swift firebase firebase-realtime-database

当我尝试使用Auth用户访问Firebase时,可以,但是当我尝试编写数据库用户的电子邮件地址时,会收到错误消息。

我的代码:

import UIKit
import Firebase

class ViewController: UIViewController {

    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var sifreField: UITextField!
    @IBOutlet weak var sifre1Field: UITextField!

    var ref = Database.database().reference()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func kayitBtn(_ sender: Any)
    {
        if(sifreField.text != sifre1Field.text)
        {
            print("****error****")
        }
        else if ( !(emailField.text?.isEmpty)! || !(sifreField.text?.isEmpty)! || !(sifre1Field.text?.isEmpty)! )
        {
            Auth.auth().createUser(withEmail: emailField.text!, password: sifreField.text!) { (user, error) in
                if error != nil
                {
                    print ("***error***")
                }
                else if ( !(self.emailField.text?.isEmpty)! || !(self.sifreField.text?.isEmpty)!  || !(self.sifre1Field.text?.isEmpty)! )
                {
                    self.ref.child("users").child((user?.uid)!).setValue(["email": self.emailField.text!])
                    print("database ok")
                }
        }
        }
    }

}

我的AppDelegate.swift(在import Firebase中添加了didFinishLaunchingWithOptions和FirebaseApp.configure()):

import UIKit
import CoreData
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        return true
    }

我的控制台错误:

enter image description here

我现在该怎么办?谢谢你的一切。

1 个答案:

答案 0 :(得分:3)

var ref: FIRDatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()
    ref = FIRDatabase.database().reference()
}

这将解决您的问题,并且该应用不会崩溃。