smstuebe / xamarin-fingerprint

Xamarin and MvvMCross plugin for authenticate a user via fingerprint sensor
Microsoft Public License
490 stars 115 forks source link

CurrentActivityResolver Error #161

Closed JEstevao closed 4 years ago

JEstevao commented 4 years ago

Hey,

I'm kinda new to the Xamarin platform so I followed Judy McNeil's video and although exist some differences from the current plugin implementation I managed to have everything working on iOS but not on Android.

So... I leave my implementation:

On MainActivity I added the ActivityRevolver:

[Activity(Label = "Shadowing1", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

And my actual code - that works fine with iOS - for fingerprint login is:

async void Bt_finger_Clicked(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (ExistingPin == null)
                {
                    await DisplayAlert("Pin Required", "Must define a Pin on first use", "Ok");
                }

                else
                {
                    //Validates that fingerprint scan exists on Device
                    bool fingerExists = await CrossFingerprint.Current.IsAvailableAsync(true);

                    if (fingerExists)
                    {
                        // Configure the fingerprint request dialog
                        var biometricConfig = new AuthenticationRequestConfiguration("Login", "Use fingerprint to login")
                        {
                            FallbackTitle = "Login",
                            CancelTitle = "Cancel"
                        };

                        //Retrieve authentication using fingerprint or device code
                        var result = await CrossFingerprint.Current.AuthenticateAsync(biometricConfig);

                        if (result.Authenticated)
                        {
                            App.Current.MainPage = new MenuPage();
                        }
                        else
                        {
                            await DisplayAlert("Authentication failed", "Login failed. Please try again.", "Ok");
                        }
                    }
                    else
                    {
                        await DisplayAlert("Fingerprint not found", "Your device doesn't have fingerprint sensor", "Ok");
                    }
                }

            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", "Something went wrong. Please try again. " + ex.ToString(), "Ok");
            }
        }

I'm having the following error:

"The configured CurrentActivityResolver returned null. You need to setup the Android implementation via CrossFingerprint.SetCurrentActivityResolver(). If you are using CrossCurrentActivity don't forget to initialize it, too!"

Simulator: Pixel 3 on Pie 9.0 (API 28)

Is it a bug? I'd say maybe it's not or others might already reported, but help is appreciated. Thanks in advanced.

smsissuechecker commented 4 years ago

Hi @JEstevao,

I'm the friendly issue checker. It seems like (100.00 %) you haven't used our issue template :cry: I think it is very frustrating for the repository owners, if you ignore them.

If you think it's fine to make an exception, just ignore this message. But if you think it was a mistake to delete the template, please close the issue and create a new one.

Thanks!

florian-berger commented 4 years ago

Hello,

before you call SetCurrentActivityResolver(Func<Activity>);, it is required to initialize the CrossCurrentActivity. Therefore you can use the following line: CrossCurrentActivity.Current.Init(this, savedInstanceState);

This should solve your problem.

JEstevao commented 4 years ago

Thank you very much @florian-berger It solved the problem! :)