firebase icon indicating copy to clipboard operation
firebase copied to clipboard

[IOS] The default FirebaseApp instance must be configured before the default Authinstance can be initialized

Open kefahB opened this issue 3 years ago • 6 comments

Hi @NathanWalker

When I call firebase().auth().addAuthStateChangeListener that will throw error

 NativeScript encountered a fatal error: Uncaught Error: The default FirebaseApp instance must be configured before the default Authinstance can be initialized. One way to ensure this is to call `FirebaseApp.configure()` in the App Delegate's `
  application(_:didFinishLaunchingWithOptions:)` (or the `@main` struct's initializer in SwiftUI).
  at
  get native(file: app/webpack:/--/node_modules/@nativescript/firebase-auth/index.ios.js:1237:0)
  at addAuthStateChangeListener(file: app/webpack:/--/node_modules/@nativescript/firebase-auth/index.ios.js:907:0)

to prevent this error I should declare init on a delegate applicationDidFinishLaunchingWithOptions then it work !

Do you have an idea what happen

Thanks

kefahB avatar Aug 20 '22 15:08 kefahB

You need to call the following before using the other packages

import { firebase } from '@nativescript/firebase-core'
firebase().initializeApp();

triniwiz avatar Aug 21 '22 18:08 triniwiz

I did that but the same result, the only work around that i've found is like I read in the Firebase docs that we should initialize firebase on a delegate

import { firebase } from '@nativescript/firebase-core'
if(isAndroid)
 {
    firebase().initializeApp();
}
import '@nativescript/firebase-auth'; // only needs to be imported 1x
import '@nativescript/firebase-messaging'; 


if (global.isIOS) {
	@NativeClass()
	@ObjCClass(UIApplicationDelegate)
	class UIApplicationDelegateImpl extends UIResponder implements UIApplicationDelegate {
		applicationContinueUserActivityRestorationHandler(application: UIApplication, userActivity: NSUserActivity, restorationHandler: (p1: NSArray<UIUserActivityRestoring>) => void): boolean {
			return handleContinueUserActivity(userActivity);
		}
		applicationOpenURLOptions(app: UIApplication, url: NSURL, options: NSDictionary<string, any>): boolean {
			return handleOpenURL(url);
        }
        applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<string, any>): boolean {
            firebase()
            .initializeApp()
            .then((app) => {
                //firebase().crashlytics().setCrashlyticsCollectionEnabled(true);
                firebase().messaging().showNotificationsWhenInForeground = true;
                console.error(firebase().messaging().isDeviceRegisteredForRemoteMessages)
                console.error(firebase().messaging().isDeviceRegisteredForRemoteMessages)
            });
            return true
        }
    }


	Application.ios.delegate = UIApplicationDelegateImpl;

}


kefahB avatar Aug 22 '22 10:08 kefahB

@kefahB is it possible to show me your use case? . Curious where the next firebase call is.

triniwiz avatar Aug 27 '22 03:08 triniwiz

Hi @triniwiz,

I've investigated the code .. the init doesn't initialize at all please see here

I didn't understand why did you put a function inside the push ? it is for some async/await stuff ?

Once you remove the function firebase init get called.

kefahB avatar Sep 22 '22 15:09 kefahB

If there is no sharedApplication usually the case when initializeApp is called early in the project the initialize method is added to a queue to be called when the app triggers the launch event then it will trigger the events on our end

triniwiz avatar Sep 22 '22 16:09 triniwiz

I've declared the init at the begin of the app.ts .. it doesn't work, neither put it in a async function!

kefahB avatar Sep 22 '22 16:09 kefahB