Reputation: 146
I'm implementing facebook login to Xamarin forms proejct using Xamarin.Auth plugin. This is ios render code.
When I click cancel button or login successfully, dismissviewcontroller is called and the web view is closed.
When I click this facebook or login button again, I'm getting below error.
Warning: Attempt to present on whose view is not in the window hierarchy!
If anyone has a good solution, please help me.
[assembly: ExportRenderer(typeof(FBLoginPage), typeof(FBLoginPageRenderer))]
namespace vidmoji.iOS
{
public class FBLoginPageRenderer : PageRenderer
{
bool IsShown;
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
if (!IsShown)
{
IsShown = true;
var auth = new OAuth2Authenticator(
clientId: App.Instance.FBAuthSettings.ClientId,
clientSecret: App.Instance.FBAuthSettings.SecurityKey,
accessTokenUrl: new Uri(App.Instance.FBAuthSettings.AccessTokenUrl),
scope: App.Instance.FBAuthSettings.Scope,
authorizeUrl: new Uri(App.Instance.FBAuthSettings.AuthorizeUrl),
redirectUrl: new Uri(App.Instance.FBAuthSettings.RedirectUrl));
auth.Completed += (sender, eventArgs) =>
{
DismissViewController(true, null);
if (eventArgs.IsAuthenticated)
{
App.Instance.loginWithFacebook(eventArgs.Account.Properties["access_token"]);
}
else {
DismissModalViewController(true);
}
};
PresentViewController(auth.GetUI(), true, null);
}
}
}
}
Upvotes: 0
Views: 479
Reputation: 9356
Updating the answer
In your FBLoginPage add a public method.
public partial class FBLoginPage : ContentPage
{
//......
public void OnAuthenticationCompleted (bool isAuthenticated, string accessToken)
{
//Get your token and do what you need to do with it.
Navigation.PopModalAsync (true);
}
}
and your renderer class change it to:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
if (!IsShown)
{
IsShown = true;
var auth = new OAuth2Authenticator(
clientId: App.Instance.FBAuthSettings.ClientId,
clientSecret: App.Instance.FBAuthSettings.SecurityKey,
accessTokenUrl: new Uri(App.Instance.FBAuthSettings.AccessTokenUrl),
scope: App.Instance.FBAuthSettings.Scope,
authorizeUrl: new Uri(App.Instance.FBAuthSettings.AuthorizeUrl),
redirectUrl: new Uri(App.Instance.FBAuthSettings.RedirectUrl));
auth.Completed += (sender, eventArgs) => {
var element = Element as LoginPage;
var token = eventArgs.IsAuthenticated ? eventArgs.Account.Properties ["access_token"] : null;
element?.OnAuthenticationCompleted (eventArgs.IsAuthenticated, token);
};
PresentViewController (auth.GetUI (), true, null);
}
}
Regarding the update.
This component is not updated since 2 years ago but the nuget package is. I suggestion to change from the the component to the Nuget package.
Good luck.
Upvotes: 1