Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The default Firebase app has not yet been configured" when it has - Swift

I made my Firebase Auth code work first and once I added Firestore add data functions everything fell apart. Here is my error message:

> 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

I clearly have configured in my AppDelegate despite this:

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
    }
}

Here's my view controller:

    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)
        }
    }

I am quite obviously new to Swift and app development in general, it's probably something really stupid. Thanks in advance!

like image 493
Maksim Tonyushkin Avatar asked Dec 17 '22 22:12

Maksim Tonyushkin


1 Answers

Try to call let db = Firestore.firestore() and let userID = Auth.auth().currentUser!.uid inside the viewDidLoad method

another solution will be to write :

lazy var db = Firestore.firestore()
lazy var userID = Auth.auth().currentUser!.uid
like image 185
Mathias Avatar answered Jan 05 '23 00:01

Mathias