使用 Firebase 身份验证时应用崩溃,原因:'Default app has already been configured.'



我正在构建我的第一个iOS应用程序,并且我正在使用firebase处理身份验证,数据库等。我添加了一个注册屏幕,并使用以下代码创建新用户:

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

当用户点击"注册"按钮上时,会有一个将它们带回原始登录视图控制器。但是,当我运行该应用程序时,它会挂在启动屏幕上。这是调试器输出:

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) 

我可以根据需要提供其他信息,任何帮助/建议都将不胜感激,因为我刚开始并希望尽可能多地学习。祝你有美好的一天!

我有一个问题消息扩展

如果您在 App扩展中建议)。

问题是:在消息扩展程序中,如果用户在线程中按几个消息打开扩展名,init(或ViewDidload)将被调用几次,因此由于Firapp.Configure()崩溃了几次...

我发现的解决方案是在主视图控制器中创建一个静态布尔:

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

我在init或viewDidload中调用firapp.configure()之前对其进行测试:

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

这样,不再崩溃。


哦,我在这里找到了一种更优雅的方法来解决问题:iOS扩展 - 致命例外:com.firebase.core默认应用已配置

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

不再以这种方式静态;)

我两次写了FIRApp.configure(),这似乎可以解决该错误。

这是有关此问题的其他解决方案。1/在" appdelegate.swift"中检查我们将看到如下

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

2/从上面的代码到

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

3/将以下代码添加到" appdelegate.swift"

override init() {
    FirebaseApp.configure()
}

4/转到" viewController.swift",然后添加代码

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

5/再次构建并运行它将起作用。谢谢!

类名称:appdelegate fcmplugin.m

[FIRAPP.CONFIGURE()];

将此方法放在最重要的情况下

- (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 if(![FIRApp defaultApp]){
    [FIRApp configure];}}

swift 4,

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

您可以在AppDelegate Init方法中调用一次以配置。

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

如果您正在使用场景委派,并且拥有不同的iOS版本,例如9,10至13 ,则必须在 appdelegate.swift中致电以这种方式:

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

scendelegate.swift 以下方式:

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

这些设置将排除错误:*

***终止应用程序由于未被发名的例外'com.firebase.core',原因:'默认应用程序已配置。'

这是因为您已经在AppDelegate中初始化了 firebaseapp.configure()请确保仅一旦配置才能进行。

以防其他人偶然发现这个问题。当使用googleSignIn SDK使用firebase SDK时。您只需要配置一次。要么做 [[GGLContext sharedInstance] configureWithError: &configureError]; 或者 [FIRApp configure]

这是我的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)
  }
}

您可以删除此FirebaseApp.configure()或添加

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

两者都为我工作

似乎团队中的某人遵循了firebase iOS设置步骤,并在appdelegate中添加了firbase.configure()。在设置中的颤抖不需要,只给出迅速而客观-C作为一种选择,这是可以理解的。

解决方案很容易删除该不必要的线路。,谢谢您的帮助。

建议在Firebase Console设置中的新开发人员更清晰。

我有同样的问题,但努力工作...

//步骤#1这是我的appdelegeate,我认为它可能是多余的,但我遇到了同样的错误。

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

在我的swiftui文件中我的函数的步骤#2在我创建用户帐户的情况下,我在auth.auth()(如下

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

对于swift 4

崩溃也有同样的问题。试图用.plist文件从firebase引用时。

(这解决了我的问题。)

尝试在ViewDidload方法之外或之内写下此内容:

1. var ref: DatabaseReference!

然后在ViewDidload方法中引用它:

2. Database.database().reference()

现在是:

FirebaseApp.configure()

而不是。

相关内容

最新更新