f-miyu / Plugin.FirebaseAuth

MIT License
61 stars 26 forks source link

[iOS] System.TypeInitializationException #28

Closed LittleCornerDev closed 3 years ago

LittleCornerDev commented 3 years ago

Hi!

Is there a good way to prevent running into a System.TypeInitializationException when initializing FirebaseAuth in Xamarin.Forms iOS?

I have the Firebase initialization call (Firebase.Core.App.Configure(firebaseOptions);) right after loading the Xamarin Forms app (LoadApplication(new App());) in AppDelegate.cs:FinishedLaunching().

The app tries to do an automatic anonymous login (if saved login data not found). It loops and waits to make sure CrossFirebaseAuth.Current is not null first, but there seems to also be a delay for CrossFirebaseAuth.Current.Instance?

System.TypeInitializationException is thrown when calling CrossFirebaseAuth.Current.Instance.SignInAnonymouslyAsync() even if CrossFirebaseAuth.Current is not null. The exception also happens when just calling CrossFirebaseAuth.Current.GetInstance() or var instance = CrossFirebaseAuth.Current.Instance.

Is there something I'm missing? Please advise! šŸ™

Error Logged

Xamarin.iOS: Received unhandled ObjectiveC exception: NSInternalInconsistencyException The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App Delegate's `application:didFinishLaunchingWithOptions:` (`application(_:didFinishLaunchingWithOptions:)` in Swift).
monotouch Warning: 0 : 
 LogIn failed: System.TypeInitializationException: The type initializer for 'Plugin.FirebaseAuth.AuthProvider' threw an exception. ---> Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App Delegate's `application:didFinishLaunchingWithOptions:` (`application(_:didFinishLaunchingWithOptions:)` in Swift).
Native stack trace:
    0   CoreFoundation                      0x00007fff20420af6 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff204209d4 -[NSException initWithCoder:] + 0
    3   ghost.iOS                           0x0000000104f54668 +[FIRAuth auth] + 88
    4   ghost.iOS                           0x000000010548f499 xamarin_dyn_objc_msgSend + 217
    5   ???                                 0x000000010fd892d6 0x0 + 4560818902

  at ObjCRuntime.Runtime.ThrowNSException (System.IntPtr ns_exception) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.8.0.3/src/Xamarin.iOS/ObjCRuntime/Runtime.cs:407 
  at ObjCRuntime.Runtime.throw_ns_exception (System.IntPtr exc) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/runtime/Delegates.generated.cs:126 
  at (wrapper native-to-managed) ObjCRuntime.Runtime.throw_ns_exception(intptr)
  at (wrapper managed-to-native) ApiDefinition.Messaging.IntPtr_objc_msgSend(intptr,intptr)
  at Firebase.Auth.Auth.get_DefaultInstance () [0x0000f] in <1f169d41f72042efa1339f3016de82fd>:0 
  at Plugin.FirebaseAuth.AuthProvider..cctor () [0x0000a] in <60c51b1d542544fb8a5aa34f4a67ce75>:0 
--- End of stack trace from previous location where exception was thrown ---

  at (wrapper managed-to-native) ApiDefinition.Messaging.IntPtr_objc_msgSend(intptr,intptr)
  at Firebase.Auth.Auth.get_DefaultInstance () [0x0000f] in <1f169d41f72042efa1339f3016de82fd>:0 
  at Plugin.FirebaseAuth.AuthProvider..cctor () [0x0000a] in <60c51b1d542544fb8a5aa34f4a67ce75>:0 
   --- End of inner exception stack trace ---
  at Plugin.FirebaseAuth.FirebaseAuthImplementation.get_Instance () [0x00000] in <60c51b1d542544fb8a5aa34f4a67ce75>:0 

p.s. Initialization and automatic login works fine on Android.

Device and OS iPhone 11, iOS 14.3

Nuget Versions Plugin.FirebaseAuth 4.1.0 Xamarin.Forms 5.0.0.19005

LittleCornerDev commented 3 years ago

I thought this issue might be related, but it seems to be for Android, and my issue is on iOS.

LittleCornerDev commented 3 years ago

Looks like calling Firebase.Core.App.Configure(firebaseOptions); earlier (before global::Xamarin.Forms.Forms.Init(); and LoadApplication(new App());) fixed the issue.

Egehanozsoy commented 3 years ago

Having the same issue and your fix doesnt seem to work for me :/