Forum Xamarin.iOS

Issue with Google Sign-in for iOS component when not using ViewController as UIDelagate

SanjogSharmaSanjogSharma USMember ✭✭
edited February 2017 in Xamarin.iOS

I'm trying to implement google sign in using this awesome component: Google Sign-in for iOS

It works great on emulator but when it comes to actual device it's crashing once i tap signin button. (iOS 10.2 - emulator is also using same OS)

After issuing SignInUser command ,It's crashing with below error (only when the app is deployed on device)

Objective-C exception thrown. Name: NSInvalidArgumentException Reason: uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.

Here is how i've initialized the GoogleSignIn

public void Configure()
        {
            NSError configureError;
            Context.SharedInstance.Configure(out configureError);

            if (configureError != null)
            {
                // If something went wrong, assign the clientID manually
                Console.WriteLine("Error configuring the Google context: {0}", configureError);
                SignIn.SharedInstance.ClientID = googleClientId;
            }

            SignIn.SharedInstance.Delegate = this;
            SignIn.SharedInstance.UIDelegate = new GoogleSignInUIDelegate();
        }

Here's my implementation of ISignInUIDelegate():

class GoogleSignInUIDelegate :  SignInUIDelegate
    {
        public override void WillDispatch(SignIn signIn, NSError error)
        {
        }
        public override void PresentViewController(SignIn signIn, UIViewController viewController)
        {
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(viewController, true, null);

        }

        public override void DismissViewController(SignIn signIn, UIViewController viewController)
        {
            UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
        }
    }

So the emulator seems to know the methods are implemented, but not the device. Any idea what i am doing wrong here?

Best Answer

  • SanjogSharmaSanjogSharma USMember ✭✭
    Accepted Answer

    After some debugging i found where the actual issue was. Somehow, the UIDelegate i assigned during initialization was lost when i was calling my login method. So i moved the line below from my initialization step to login

    SignIn.SharedInstance.UIDelegate = new GoogleSignInUIDelegate();

    Here's how my login method looks like now:

        public void Login()
        {
            SignIn.SharedInstance.UIDelegate = new GoogleSignInUIDelegate(); //moved this here from Configure
            SignIn.SharedInstance.SignInUser();
        }
    

    This took care of the issue for me but i am still not sure why this is only an issue on the device and not the emulator. Any Ideas?

Answers

  • SanjogSharmaSanjogSharma USMember ✭✭
    Accepted Answer

    After some debugging i found where the actual issue was. Somehow, the UIDelegate i assigned during initialization was lost when i was calling my login method. So i moved the line below from my initialization step to login

    SignIn.SharedInstance.UIDelegate = new GoogleSignInUIDelegate();

    Here's how my login method looks like now:

        public void Login()
        {
            SignIn.SharedInstance.UIDelegate = new GoogleSignInUIDelegate(); //moved this here from Configure
            SignIn.SharedInstance.SignInUser();
        }
    

    This took care of the issue for me but i am still not sure why this is only an issue on the device and not the emulator. Any Ideas?

  • calincozmincalincozmin Member ✭✭

    @SanjogSharma there is any chance to post an entire flow of how you manage to sign in the user into google account ? By flow, I mean the entire code. Thanks

  • batmacibatmaci DEMember ✭✭✭✭✭

    answer is outdated. anyone looking for a new versions i on the link

    https://github.com/xamarin/GoogleApisForiOSComponents/issues/331

Sign In or Register to comment.