尝试在使用OAuth演示者时显示UINavigationController

时间:2017-08-11 04:31:52

标签: oauth xamarin.ios uinavigationcontroller xamarin.forms warnings

我正在整合Facebook登录Xamarin PCL。我已经按照示例“漫画书”https://github.com/moljac/Xamarin.Auth.Samples.NugetReferences并将登录过程集成到我的应用程序中。

但是,我从Xamarin PCL项目调用oauth演示者以在iOS上的应用程序中显示Facebook UI时,我得到以下异常。我已经提供了下面使用的代码来调用Facebook UI进行登录。

  

错误:   警告:尝试在Xamarin_Forms_Platform_iOS_PlatformRenderer:0x7ffa00576b80上显示UINavigationController:0x7ffa01275a00,其视图不在窗口层次结构中!

代码:

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); 

如何解决此错误?

注意:相同的代码适用于Android。我只在iOS中遇到此问题。我正在使用Xamarin Auth v1.5.0.0

1 个答案:

答案 0 :(得分:2)

通过在导航堆栈中堆叠应用程序中的页面来解决问题。