【问题标题】:Attempt to present UINavigationController while using OAuth presenter尝试在使用 OAuth 演示器时呈现 UINavigationController
【发布时间】:2017-08-11 04:31:52
【问题描述】:

我正在为 Xamarin PCL 集成 Facebook 登录。我遵循了示例“漫画书”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

【问题讨论】:

  • 通过将我的应用程序中的页面堆叠到导航堆栈来解决问题。似乎使用 PushModalAsync() 与 Xamarin Auth 一起导航到页面会产生此问题。通过用 PushAsync() 替换它并将我的主应用程序包装到 NavigationPage(),解决了这个问题。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多