Piyush Dubey Piyush Dubey - 5 months ago 540
iOS Question

Custom Google Sign-In button - iOS

I want to customize Google Sign-In button like below:-

enter image description here

I have tried below links, but none of them helped really much:-
How to customize google sign in button?
https://developers.google.com/identity/sign-in/ios/

Could somebody please guide what I should do? I can't use Google+ Sign-In button because "Google+ Sign-In is deprecated".

Edited:- I tried the code provided on below link:-

https://developers.google.com/identity/sign-in/ios/sign-in#add_the_sign-in_button

Answer

You can add your own button instead of using Google Sign-In button Do follwing things

Objective C Version

1)Add your own button into storyBoard

2)drag action into viewController

- (IBAction)googlePlusButtonTouchUpInside:(id)sender {
            [[GIDSignIn sharedInstance] signIn];
  }

3)handle delegate methods

#pragma mark - Google SignIn Delegate

- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {

  }

// Present a view that prompts the user to sign in with Google

- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
{
    [self presentViewController:viewController animated:YES completion:nil];
}

// Dismiss the "Sign in with Google" view

- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];

}

//completed sign In

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
//user signed in
//get user data in "user" (GIDGoogleUser object)
}

Swift 2 Version

In Swift make sure you have added briding header as the library is written in objective C

1)Add your own button into storyBoard

2)drag action into viewController

@IBAction func googlePlusButtonTouchUpInside(sender: AnyObject) {
      GIDSignIn.sharedInstance().signIn()
} 

3)handle delegate methods

//MARK:Google SignIn Delegate

func signInWillDispatch(signIn: GIDSignIn!, error: NSError!) {
  myActivityIndicator.stopAnimating()
}

// Present a view that prompts the user to sign in with Google

func signIn(signIn: GIDSignIn!,
    presentViewController viewController: UIViewController!) {
  self.presentViewController(viewController, animated: true, completion: nil)
}

// Dismiss the "Sign in with Google" view

func signIn(signIn: GIDSignIn!,
    dismissViewController viewController: UIViewController!) {
  self.dismissViewControllerAnimated(true, completion: nil)
}

//completed sign In

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
  withError error: NSError!) {
    if (error == nil) {
      // Perform any operations on signed in user here.
      let userId = user.userID                  // For client-side use only!
      let idToken = user.authentication.idToken // Safe to send to the server
      let fullName = user.profile.name
      let givenName = user.profile.givenName
      let familyName = user.profile.familyName
      let email = user.profile.email
      // ...
    } else {
      print("\(error.localizedDescription)")
    }
}