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

IosSwiftFirebaseFirebase Authentication

Ios Problem Overview


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 user:

FIRAuth.auth()?.createUserWithEmail(emailAddress.text!, password: password.text!, completion: { (user, error) in
            
        })

When the user taps on the sign up button, there is a segue that should take them back to the original login view controller. However, when I got to run the app, it hangs on the launch screen. Here is the debugger output:

2016-06-19 14:35:05.402 unitaskr[4386:82981] Configuring the default app.
2016-06-19 14:35:05.413 unitaskr[4386:] <FIRAnalytics/INFO> Firebase Analytics     v.3200000 started
2016-06-19 14:35:05.414 unitaskr[4386:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see ...)
2016-06-19 14:35:05.419: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2016-06-19 14:35:05.418 unitaskr[4386:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist
2016-06-19 14:35:05.430 unitaskr[4386:82981] *** Terminating app due to uncaught exception 'com.firebase.core', reason: 'Default app has already been configured.'
*** First throw call stack:
(
0   CoreFoundation                      0x00000001100a8d85   __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001108e7deb objc_exception_throw + 48
2   CoreFoundation                      0x00000001100a8cbd +[NSException raise:format:] + 205
3   unitaskr                            0x000000010b58844d +[FIRApp    configureDefaultAppWithOptions:sendingNotifications:] + 102
4   unitaskr                            0x000000010b588238 +[FIRApp configure] + 302
5   unitaskr                            0x000000010b541f1a _TFC8unitaskr11AppDelegate11applicationfTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVs10DictionaryCSo8NSObjectPs9AnyObject____Sb + 266
6   unitaskr                            0x000000010b542204 _TToFC8unitaskr11AppDelegate11applicationfTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVs10DictionaryCSo8NSObjectPs9AnyObject____Sb + 180
7   UIKit                               0x000000010e5bf9ac -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 272
8   UIKit                               0x000000010e5c0c0d -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3415
9   UIKit                               0x000000010e5c7568 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1769
10  UIKit                               0x000000010e5c4714 -[UIApplication workspaceDidEndTransaction:] + 188
11  FrontBoardServices                  0x00000001127b78c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
12  FrontBoardServices                  0x00000001127b7741 -[FBSSerialQueue _performNext] + 178
13  FrontBoardServices                  0x00000001127b7aca -[FBSSerialQueue _performNextFromRunLoopSource] + 45
14  CoreFoundation                      0x000000010ffce301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15  CoreFoundation                      0x000000010ffc422c __CFRunLoopDoSources0 + 556
16  CoreFoundation                      0x000000010ffc36e3 __CFRunLoopRun + 867
17  CoreFoundation                      0x000000010ffc30f8 CFRunLoopRunSpecific + 488
18  UIKit                               0x000000010e5c3f21 -[UIApplication _run] + 402
19  UIKit                               0x000000010e5c8f09 UIApplicationMain + 171
20  unitaskr                            0x000000010b542a42 main + 114
21  libdyld.dylib                       0x00000001113b692d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I can provide additional information as needed, any help/advice would be greatly appreciated as I am just starting out and looking to learn as much as possible. Have a great day!

Ios Solutions


Solution 1 - Ios

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 : https://stackoverflow.com/questions/38536257/ios-extension-fatal-exception-com-firebase-core-default-app-has-already-been

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

No more static this way ;)

Solution 2 - Ios

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

Solution 3 - Ios

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

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

For Swift 4,

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

Solution 6 - Ios

You can call once in AppDelegate init method to configure.

override init() {
   // Firebase Init
   FIRApp.configure()
}

Solution 7 - Ios

If you're using Scene delegate from and you have different iOS versions like 9, 10 up to 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.'

Solution 8 - Ios

just in case someone else stumble upon this problem. when use firebase sdk with googleSignIn sdk. you only have to configure them once. either do

 [[GGLContext sharedInstance] configureWithError: &configureError];

or

[FIRApp configure]

Solution 9 - Ios

Here is my code for AppDelegate.swift

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
  FirebaseApp.configure()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

You can remove this this FirebaseApp.configure() or add

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

both are working for me

Solution 10 - Ios

It's because you have already initialized FirebaseApp.configure() in the AppDelegate. Please make sure it's only configured once and the error should go.

Solution 11 - Ios

Seems like someone in the team had followed the firebase Ios setup steps and added the line Firebase.configure() in the AppDelegate.swift thinking it was needed for confirguration as it doesn't state that it's not needed for flutter in setup and only gives swift and objective-c as an option, which is understandable to confuse some people.

Solution was simple to remove that unnecessary line. , Thank you for the help.

Would recommend making it clearer for new developers in the firebase console setup.

Solution 12 - Ios

For Swift 4

Had the same problem with crashing. When trying to reference from Firebase with a .plist file.

(This fixed my problem.)

Try writing this outside of or within the viewDidLoad method:

1. var ref: DatabaseReference!

Then reference it within the viewDidLoad method:

2. Database.database().reference()

And now it's:

FirebaseApp.configure()

Instead.

Solution 13 - Ios

I had same problem but got it working...

// Step #1 Here is my AppDelegeate I think it may be redundant but I got it working and had the same error.

class AppDelegate: NSObject, UIApplicationDelegate {
    override init() {
       // Firebase Init
       FirebaseApp.configure()
    }
  func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions:
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    return true
  }
}

Step #2 for my function in my swiftui file where I create user account I add the 'if' statement before the Auth.auth() like such:

private func createNewCount(){
    if FirebaseApp.app() == nil {
           FirebaseApp.configure()
       }
    Auth.auth().createUser(withEmail: email, password: password) { result, err in
        if let err = err {
            print("Failed to create user", err)
            return
        }
        print("succesfully created user \(result?.user.uid ?? "")")
    }
    
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrenden BelluardoView Question on Stackoverflow
Solution 1 - IosChristian NavelotView Answer on Stackoverflow
Solution 2 - IosBrenden BelluardoView Answer on Stackoverflow
Solution 3 - IosKhoem VichetView Answer on Stackoverflow
Solution 4 - IosPriyank GujaratiView Answer on Stackoverflow
Solution 5 - Iosssowri1View Answer on Stackoverflow
Solution 6 - IosOurangZeb KhanView Answer on Stackoverflow
Solution 7 - IosMihail SalariView Answer on Stackoverflow
Solution 8 - IosIan ZhaoView Answer on Stackoverflow
Solution 9 - IosAbir AhsanView Answer on Stackoverflow
Solution 10 - IosDenver View Answer on Stackoverflow
Solution 11 - IosJithin U. AhmedView Answer on Stackoverflow
Solution 12 - IosAlexander KindströmView Answer on Stackoverflow
Solution 13 - IosrobertuxoView Answer on Stackoverflow