ios 使用 Firebase 身份验证时应用崩溃,原因:“默认应用已配置。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37910766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:28:08  来源:igfitidea点击:

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

iosswiftfirebasefirebase-authentication

提问by Brenden Belluardo

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:

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

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:

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

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!

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

回答by Christian Navelot

I had a problem with a Messages Extension:

我遇到了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).

如果您在App Extension 中,则没有委托,您必须将 FIRApp.configure() 放在主 ViewController 的 init 中(或按照建议放在 viewDidLoad 中)。

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

问题是:在消息扩展中,如果用户在打开扩展的线程中按下几条消息,init(或 viewdidLoad)将被多次调用,因此由于多次调用 FIRApp.configure() 而崩溃......

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

我找到的解决方案是在主视图控制器中创建一个静态 bool :

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

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

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

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

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

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

No more static this way ;)

这样就不再是静态的了 ;)

回答by Brenden Belluardo

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

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

回答by Khoem Vichet

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

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

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

2/从上面的代码中删除“FirebaseApp.configure()”到

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"

3/ 将以下代码添加到“AppDelegate.swift”中

override init() {
    FirebaseApp.configure()
}

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

4/转到“ViewController.swift”并添加代码

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

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

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

回答by Priyank Gujarati

Class Name: AppDelegate+FCMPlugin.m

类名:AppDelegate+FCMPlugin.m

[FIRApp.configure()];

[FIRApp.configure()];

Put this at the top most of this method

把这个放在这个方法的最上面

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

回答by Ourang-Zeb Khan

You can call once in AppDelegate init method to configure.

您可以在 AppDelegate init 方法中调用一次进行配置。

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

回答by ssowri1

For Swift 4,

对于 Swift 4,

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

回答by Ian Zhao

just in case someone else stumble upon this problem. when use firebasesdk with googleSignInsdk. you only have to configure them once. either do [[GGLContext sharedInstance] configureWithError: &configureError]; or [FIRApp configure]

以防万一其他人偶然发现这个问题。将firebasesdk 与googleSignInsdk一起使用时。您只需配置一次。要么做 [[GGLContext sharedInstance] configureWithError: &configureError]; 要么 [FIRApp configure]

回答by Alexander Kindstr?m

For Swift 4

对于 Swift 4

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

有同样的崩溃问题。尝试使用 .plist 文件从 Firebase 引用时。

(This fixed my problem.)

(这解决了我的问题。)

Try writing this outside of or within the viewDidLoad method:

尝试在 viewDidLoad 方法外部或内部编写此代码:

1. var ref: DatabaseReference!

Then reference it within the viewDidLoad method:

然后在 viewDidLoad 方法中引用它:

2. Database.database().reference()

And now it's:

现在是:

FirebaseApp.configure()

Instead.

反而。