Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to present UINavigationController while using OAuth presenter

I am integrating the Facebook login for Xamarin PCL. I have followed the sample "Comic book" https://github.com/moljac/Xamarin.Auth.Samples.NugetReferences and integrated the login process into my application.

But, I am getting the below exception while the oauth presenter is called from Xamarin PCL project to present the Facebook UI in the app on iOS. I have provided the code i used below to call the Facebook UI for login.

Error: Warning: Attempt to present UINavigationController: 0x7ffa01275a00 on Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x7ffa00576b80 whose view is not in the window hierarchy!

Code:

var auth = new OAuth2Authenticator(
                clientId: FacebookClientId,
                scope: "email", 
                authorizeUrl: new Uri(FacebookAuthorizeUrl),
                redirectUrl: new Uri(FacebookRedirectUri));
                auth.AllowCancel = true;
                auth.Completed += async (s, es) =>
                {
                    if (es.IsAuthenticated)
                    {
                        var request = new OAuth2Request(
                        "GET",
                        new Uri("https://graph.facebook.com/me?fields=email,name,picture,cover,birthday"),
                        null,
                        es.Account);
                        var fbResponse = await request.GetResponseAsync();
                        if (fbResponse != null)
                        {
                                              var fbUser = JsonValue.Parse(fbResponse.GetResponseText()); // Getting the error for System.Json double exception issue as well for OAuth v1.5.0.0 only in iOS.
                            if (fbUser != null)
                            {
                                LoginViewModel loginViewModel = new LoginViewModel();
                                if (!string.IsNullOrEmpty(fbUser["name"]))
                                {
                                    loginViewModel.ProfileName = fbUser["name"];
                                    loginViewModel.UserName = fbUser["name"];
                                }

                            LoginViewModel.SelfLoginViewModel = loginViewModel;
                            await this.Navigation.PushModalAsync(new MasterHome());
                        }
                        else
                        {
                            await DisplayAlert("Message", "Check your internet connection!", "OK");
                        }
                    }
                    else
                    {
                        await DisplayAlert("Message", "Check your internet connection!", "OK");
                    }
                }
            };
            auth.Error += (s, ea) =>
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Error= ").AppendLine($"{ea.Message}");
                DisplayAlert("Authentication Error", sb.ToString(),"OK");
            };

            Xamarin.Auth.Presenters.OAuthLoginPresenter presenter = null;
            presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
            presenter.Login(auth); 

How can I resolve this error?

Note: The same code works perfectly for Android. I am facing this issue only in iOS. I am using Xamarin Auth v1.5.0.0

like image 887
Vijay457 Avatar asked Oct 30 '22 03:10

Vijay457


1 Answers

Issue resolved by Stacking the pages in the application in Navigation Stack.

like image 103
Vijay457 Avatar answered Nov 11 '22 17:11

Vijay457