xamarin / GoogleApisForiOSComponents

MIT License
225 stars 156 forks source link

Xamarin.Google.iOS.SignIn 5.0.2.4 does not call ISignInUIDelegate methods #629

Open Zhangos opened 9 months ago

Zhangos commented 9 months ago

Xamarin.Google.iOS.SignIn version 5.0.2.4 does not call ISignInUIDelegate methods when calling SignIn.SharedInstance.SignInUser();

 var topViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
 if (topViewController.PresentedViewController != null)
       topViewController = topViewController.PresentedViewController;
 if (topViewController is UINavigationController navigationController)
       topViewController = navigationController.TopViewController;
 Google.SignIn.SignIn.SharedInstance.PresentingViewController = topViewController;
 Google.SignIn.SignIn.SharedInstance.Delegate = new GoogleSignInDelegate(signInResponse, googleTimer);
 Google.SignIn.SignIn.SharedInstance.SignInUser();
    class GoogleSignInDelegate : NSObject, ISignInDelegate
    {
        private Timer googleTimer;
        private readonly TaskCompletionSource<WebAuthenticatorResult> signInResponse;
        public GoogleSignInDelegate(TaskCompletionSource<WebAuthenticatorResult> signInResponse, Timer googleTimer)
        {
            this.signInResponse = signInResponse;
            this.googleTimer = googleTimer;
        }
        public async void DidSignIn(SignIn signIn, GoogleUser user, NSError error)
        {
            System.Diagnostics.Debug.Write("GoogleSignInDelegate >>>>> call DidSignIn");
            if (error != null)
            {
                if (error.Code == -5)
                {
                    signInResponse?.TrySetCanceled();
                }
                else
                {
                    signInResponse?.TrySetException(new Exception(error.Description));
                }
                System.Diagnostics.Debug.Write($"GoogleSignInDelegate error>>>>> {error.Description}");
                return;
            }
            var authenticatorResult = new WebAuthenticatorResult();
            authenticatorResult.Properties.Add("code", user.ServerAuthCode);
            signInResponse?.TrySetResult(authenticatorResult);
        }
    }
alexshikov commented 8 months ago

I have the same issue using Google.SignIn.SignIn.SharedInstance.SignedIn += OnSignedIn. It's never called and there are no errors or warning in the console.

@Zhangos have you been able to find a workaround or some ideas why it happens?

alexshikov commented 8 months ago

Surprisingly using SharedInstance.Delegate = this instead of SharedInstance.SignedIn += OnSignedIn started to work for me, where this implements ISignInDelegate.