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

Solution 1:

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

Solution 2:

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

Solution 3:

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!

Solution 4:

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

Solution 5:

For Swift 4,

if FirebaseApp.app() == nil {
/// code snippet
}