TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

MauiProgram.cs FinishedLaunching event is not defined. IOS App Crashes #116

Closed priyarishi closed 1 year ago

priyarishi commented 1 year ago

In the MauiProgram.cs , ios event has been defined as ios.FinishedLaunching . But the FinshedLaunching event is never defined. How should we define that for the iphone build to work fine. iphone Build Crashes.

priyarishi commented 1 year ago

@TobiasBuchholz Can someone please help me with ios issue. App crashes on ios when using this. Do I need to define this event in some other way. Need help please.

TobiasBuchholz commented 1 year ago

Do you use the compiler directive IOS for the code that calls events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => ...?

    private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                CrossFirebase.Initialize(app, launchOptions, CreateCrossFirebaseSettings());
                return false;
            }));
#else
            events.AddAndroid(android => android.OnCreate((activity, state) =>
                CrossFirebase.Initialize(activity, state, CreateCrossFirebaseSettings())));
#endif
        });

Maybe this screen shot clarifies things:

Screenshot 2023-01-19 at 22 20 29

Furthermore you can take a look at the sample project. Hope this helps!

priyarishi commented 1 year ago

@TobiasBuchholz Thanks for responding. I have checked all the settings now. But still the same issue. The exact error is "System.TypeLoadException: 'Could not load type of field 'Plugin.Firebase.Auth.FirebaseAuthImplementation:_firebaseAuth' (0) due to: Could not resolve type with token 01000085 from typeref (expected class 'Firebase.Auth.Auth' in assembly 'Firebase.Auth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null') assembly:Firebase.Auth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null type:Firebase.Auth.Auth member:(null)'"

Can you please tell me if I am missing something.

priyarishi commented 1 year ago

@TobiasBuchholz Same issue comes when I try to run the sample app on ios Simulator.

priyarishi commented 1 year ago

@TobiasBuchholz Can you please guide on this specific error. I have been trying to run this on ios simulator. It runs perfectly on android . I am not finding a fix or whats wrong with iOS. Ran the sample project as well. Same error.

vhugogarcia commented 1 year ago

@TobiasBuchholz Can you please guide on this specific error. I have been trying to run this on ios simulator. It runs perfectly on android . I am not finding a fix or whats wrong with iOS. Ran the sample project as well. Same error.

Could you please share a project demo where you can reproduce the issue on iOS, please? it may be helpful for @TobiasBuchholz 👍🏻

priyarishi commented 1 year ago

@vhugogarcia @TobiasBuchholz I am using the Playground sample app attached in the github repo. It runs fine on simulator and then crashes on the device. Exact Error Log looks like this [0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.Firebase.Auth.FirebaseAuthImplementation.Initialize(UIApplication application, NSDictionary launchOptions, String facebookAppId, String facebookDisplayName) at Plugin.Firebase.iOS.CrossFirebase.Initialize(UIApplication app, NSDictionary options, CrossFirebaseSettings settings, Options firebaseOptions, String name)

priyarishi commented 1 year ago

@vhugogarcia @TobiasBuchholz Please see attached sample I am using. which runs fine on the simulator but crashes on an iphone device. Please help.

SSOMAUI.zip

TobiasBuchholz commented 1 year ago

According to the .csrpoj file in your .zip file you are setting the ApplicationId to 6PM7VH3MS5.com.companyname.ssomaui:

...
<ApplicationId>6PM7VH3MS5.com.companyname.ssomaui</ApplicationId>
...

I think it just needs to be com.companyname.ssomaui without the prepending 6PM7VH3MS5., exactly like the value for BUNDLE_ID in the GoogleService-Info.plist. Furthermore in your Info.plist there is an empty value for CFBundleIdentifier, you should probably delete that or also set it to com.companyname.ssomaui.

priyarishi commented 1 year ago

@TobiasBuchholz I initially had the ApplicationId as what you suggested i.e com.companyname.ssomaui But it created a problem deploying app on the iphone. So this is the provisioning profile identifier created automatically when in sync with the apple developer account.

Also , this works fine on Android when using Firebase for SSO. So is it something which is apple specific you think? I tried providing the CFBundleIdentifier in Info.plist , but the same issue is happening.

Application ID makes the app work fine on an iphone device when I remove the Firebase Initialization from the app. The sample app provided in the Github , did you test on the iphone device? Were you able to run it?

TobiasBuchholz commented 1 year ago

Yes, I've tested iOS and android on simulator and device and everything is working fine for the Playground project as well as for the Plugin.Firebase.IngetrationTests project.

According to your error messages when calling CrossFirebase.Initialize(app, launchOptions, CreateCrossFirebaseSettings()) app seems to be null which it shouldn't, so maybe there is a general problem with your setup? Could you create a new project without a reference to Plugin.Firebase and add the following code to your MauiProgramm:

builder.ConfigureLifecycleEvents(events => {
    events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
        // is app or launchOptions null?
        return false;
    }));

If in this case app is null when deployed to a device then this issue isn't related to the plugin.

priyarishi commented 1 year ago

@TobiasBuchholz I created a new project . Did not add Plugin.Firebase. Added the ConfigureLifecycle like you mentioned above. The app launches fine on iPhone device. When I placed the breakpoint , app is not null . launchOptions is null. But it launches and open the demo screen fine.

priyarishi commented 1 year ago

@TobiasBuchholz I revisited the firebase options all over again. But the setup of Firebase should not be an issue as it is working and calling the google SSO fine on simulator fine. I am not able to figure out whats wrong.

priyarishi commented 1 year ago

@TobiasBuchholz Can you share the sample app you are testing with as of now. Maybe I am able to figure out the missing things. Thanks in advance.

TobiasBuchholz commented 1 year ago

@TobiasBuchholz I created a new project . Did not add Plugin.Firebase. Added the ConfigureLifecycle like you mentioned above. The app launches fine on iPhone device. When I placed the breakpoint , app is not null . launchOptions is null. But it launches and open the demo screen fine.

Ok, and after adding the plugin to your project without any other changes app is null now? If not I would suggest going step by step until app changes to null. Maybe this can help us figure out what the problem could be. The code of the sample app in the repo is up to date.

priyarishi commented 1 year ago

@TobiasBuchholz I tried the same steps. Installed the Plugin.Firebase nuget in the project. When you come to the events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => app is still non null and launchoptions is null like before. The App renders fine on the iPhone device.

Then I added CrossFirebase.Initialize(app, launchOptions, CreateCrossFirebaseSettings()); inside the event as the next step. The app crashes again on this line. I put a breakpoint here. the app is not null and the launchOptions is null. Why is launchOptions null in my case. Does it have a null value for you as well? What can be done?

TobiasBuchholz commented 1 year ago

Okay, I've assumed app would be null and cause the problems in your case, but apparently it isn't...launchOptions is null for me as well, so that's not the problem.

The following code is called in FirebaseAuthImplementation.Initialize():

public static void Initialize(UIApplication application, NSDictionary launchOptions, string facebookAppId, string facebookDisplayName)
{
    var googleServiceDictionary = NSDictionary.FromFile("GoogleService-Info.plist");
    SignIn.SharedInstance.ClientId = googleServiceDictionary["CLIENT_ID"].ToString();

    Settings.AppId = facebookAppId;
    Settings.DisplayName = facebookDisplayName;
    ApplicationDelegate.SharedInstance.FinishedLaunching(application, launchOptions);
}

So my best guess would be that the problem is related to your GoogleService-Info.plist setup, but I have no idea why it could work on simulator and not on the device, sorry.

angelru commented 1 year ago

I added

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0-ios'">
     <BundleResource Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />
</ItemGroup>

but I still can't run the app on an iOS device with windows. In the emulator yes.

any ideas? @TobiasBuchholz @priyarishi

AlleSchonWeg commented 1 year ago

Running the App via HotRestart (iPhone device with windows) has limitations: https://learn.microsoft.com/en-us/dotnet/maui/ios/hot-restart?view=net-maui-7.0#prevent-code-from-executing or XF doc: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/deploy-test/hot-restart#limitations

One limitation is that static frameworks are not supported. afaik the firebase constains static librarys: https://github.com/firebase/firebase-ios-sdk/discussions/10532 .

TobiasBuchholz commented 1 year ago

Since I haven't heard from you in more than 30 days, I hope this issue is no longer affecting you. If it is, please reopen this issue and provide the more information so that I can look into it further. Thank you!