App crashing when using Firebase Auth, reason: 'Default app has already been configured.'

前端 未结 9 1650
梦如初夏
梦如初夏 2020-12-13 23:34

I\'m building my first iOS application, and I am using Firebase to handle authentication, database, etc. I added a sign up screen and used the following code to create a new

相关标签:
9条回答
  • 2020-12-14 00:13

    For Swift 4,

    if FirebaseApp.app() == nil {
    /// code snippet
    }
    
    0 讨论(0)
  • 2020-12-14 00:20

    This is other solution about this issue. 1/ Check in the "AppDelegate.swift" we will see as below

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

    2/ Remove "FirebaseApp.configure()" from above code to

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

    3/ Add below code into "AppDelegate.swift"

    override init() {
        FirebaseApp.configure()
    }
    

    4/ Go to "ViewController.swift" and add the code

        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }
    

    5/ Build again and Run it's will work. Thanks!

    0 讨论(0)
  • 2020-12-14 00:21

    If you're using Scene delegate from and you have more iOS versions like 9, 10 until 13, you must call in AppDelegate.swift in this way:

     if #available(iOS 13.0, *) {
     
     } else {
         FirebaseApp.configure()
     }
    

    And in SceneDelegate.swift this way:

     if #available(iOS 13.0, *) {
       FirebaseApp.configure()
     }
    

    These settings will exclude error like: *

    *** Terminating app due to uncaught exception 'com.firebase.core', reason: 'Default app has already been configured.'

    0 讨论(0)
  • 2020-12-14 00:22

    Class Name: AppDelegate+FCMPlugin.m

    [FIRApp.configure()];

    Put this at the top most of this method

    - (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     if(![FIRApp defaultApp]){
        [FIRApp configure];}}
    
    0 讨论(0)
  • 2020-12-14 00:24

    I wrote FIRApp.configure() twice, that seemed to fix the error.

    0 讨论(0)
  • 2020-12-14 00:26

    I had a problem with a Messages Extension :

    If you're in an App Extension, you have no delegate, and you'll have to put the FIRApp.configure() in the init of your main ViewController (or in the viewDidLoad as suggested).

    Problem is : in a Messages Extension, if the user presses several messages in the thread opening your extension, init (or viewdidLoad) will be called several times, therefore crashing due to FIRApp.configure() called several times...

    The solution I found was to create a static bool in the main View Controller :

        static var isAlreadyLaunchedOnce = false // Used to avoid 2 FIRApp configure
    

    and I test it before calling FIRApp.configure() in the init or viewDidLoad :

    // Configure Firebase
        // ------------------
        // We check if FIRApp has already been configured with a static var, else it will crash...
        if !MessagesViewController.isAlreadyLaunchedOnce {
            FIRApp.configure()
    
            MessagesViewController.isAlreadyLaunchedOnce = true
        }
    

    This way, no more crashes.


    Oh, I found a much more elegant way to solve the problem here : iOS Extension - Fatal Exception: com.firebase.core Default app has already been configured

        // Configure Firebase
        // ------------------
        if FIRApp.defaultApp() == nil {
            FIRApp.configure()            
        }
    

    No more static this way ;)

    0 讨论(0)
提交回复
热议问题