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
For Swift 4,
if FirebaseApp.app() == nil {
/// code snippet
}
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!
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.'
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];}}
I wrote FIRApp.configure()
twice, that seemed to fix the error.
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 ;)