xamarin / GoogleApisForiOSComponents

MIT License
225 stars 161 forks source link

MAUI iOS configure errors #577

Open balazs-adam opened 1 year ago

balazs-adam commented 1 year ago

Hi,

In my MAUI application targeting net6.0-ios15.4 I configure the Firebase App the following way in the AppDelegate

public bool FinishedLaunching(UIKit.UIApplication application, NSDictionary launchOptions)
{
   Firebase.Core.App.Configure(new Firebase.Core.Options("googleAppId", "gcmSenderId") { ... });

   Crashlytics.SharedInstance.Init();
   Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true);
   Crashlytics.SharedInstance.SendUnsentReports();
   return base.FinishedLaunching(application, launchOptions);
}

But It seems that I can't instantiate the Firebase.Core.Options class because I get the following exception:

[0:] An error occurred: 'Could not create an native instance of the type 'Firebase.Core.Options': the native class hasn't been loaded.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.'. Callstack: '   at Foundation.NSObject.InitializeObject(Boolean alloced)
   at Foundation.NSObject..ctor(NSObjectFlag x)
   at Firebase.Core.Options..ctor(String googleAppId, String gcmSenderId)
   at MauiApp5.AppDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions) in C:\Source\MauiApp5\MauiApp5\Platforms\iOS\AppDelegate.cs:line 14
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at MauiApp5.Program.Main(String[] args) in C:\Source\MauiApp5\MauiApp5\Platforms\iOS\Program.cs:line 22
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)'

As a workaround, I've also tried to initialize the Firebase App using a GoogleService-Info.plist file in the Platforms/iOS folder and adding the following to the csproj:

<ItemGroup>
  <BundleResource Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />
</ItemGroup>

I've verified that the plist file does get included in the ipa output and the Firebase.Core.App.Configure call runs without errors. But when I'm trying to call Crashlytics.SharedInstance.Init() I get a NullReferenceException on the SharedInstance. Also after the Firebase.Core.App.Configure call the Firebase.Core.App.DefaultInstance stays null.

public bool FinishedLaunching(UIKit.UIApplication application, NSDictionary launchOptions)
{
   Firebase.Core.App.Configure();

   // both Firebase.Core.App.DefaultInstance and Crashlytics.SharedInstance are null here!
   Crashlytics.SharedInstance.Init();
   Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true);
   Crashlytics.SharedInstance.SendUnsentReports();
   return base.FinishedLaunching(application, launchOptions);
}

I've tested it in an empty MAUI project adding only the following nugets:

Anyone else experiencing a similar issue?

mbilalbenli commented 1 year ago

Hi, I am facing the similar issue with Auth.

//DefaultInstance is null.
Firebase.Auth.Auth instance = Firebase.Auth.Auth.DefaultInstance;
mbilalbenli commented 1 year ago

This only happens when using Hot Restart. When I am deploying by connecting with mac, this issue disappeared.

AlleSchonWeg commented 1 year ago

Hot Restart supports only dynamic libraries: https://learn.microsoft.com/en-us/dotnet/maui/deployment/hot-restart#prevent-code-from-executing I think thats the problem.

RomanDrechsel commented 1 year ago

Same problem here.

Is there any fix yet?

mbilalbenli commented 1 year ago

Same problem here.

Is there any fix yet?

Workaround: Try to deploy by connect with mac. For GoogleService-Info.plist file use the @balazs-adam's workaround.

balazs-adam commented 1 year ago

Yes, for now I'd recommend using the HOTRESTART preprocessor to conditionally initialize the FirebaseApp only when Hot Restart is not enabled.