Integrating Google Sign-In into your iOS app

Google Sign-In manages the OAuth 2.0 flow and token lifecycle, simplifying your integration with Google APIs.

Before you begin

Download the dependencies and configure your Xcode project.

Enable sign-in

To enable sign in, you must configure the GGLContext shared instance (or, if you manually installed the SDK, the GIDSignIn shared instance). You can do this in many places in your app. Often the easiest place to configure this instance is in your app delegate's application:didFinishLaunchingWithOptions: method.

  1. In your app delegate's .h file, declare that this class implements the GIDSignInDelegate protocol.
    #import <Google/SignIn.h>
    @interface AppDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>
    
  2. In your app delegate's application:didFinishLaunchingWithOptions: method, configure the GGLContext shared instance and set the sign-in delegate.
    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      NSError* configureError;
      [[GGLContext sharedInstance] configureWithError: &configureError];
      NSAssert(!configureError, @"Error configuring Google services: %@", configureError);
    
      [GIDSignIn sharedInstance].delegate = self;
    
      return YES;
    }
    
  3. Implement the application:openURL:options: method of your app delegate. The method should call the handleURL method of the GIDSignIn instance, which will properly handle the URL that your application receives at the end of the authentication process.

    For your app to run on iOS 8 and older, also implement the deprecated application:openURL:sourceApplication:annotation: method.

  4. In the app delegate, implement the GIDSignInDelegate protocol to handle the sign-in process by defining the following methods:
    - (void)signIn:(GIDSignIn *)signIn
    didSignInForUser:(GIDGoogleUser *)user
         withError:(NSError *)error {
      // Perform any operations on signed in user here.
      NSString *userId = user.userID;                  // For client-side use only!
      NSString *idToken = user.authentication.idToken; // Safe to send to the server
      NSString *fullName = user.profile.name;
      NSString *givenName = user.profile.givenName;
      NSString *familyName = user.profile.familyName;
      NSString *email = user.profile.email;
      // ...
    }
    
    - (void)signIn:(GIDSignIn *)signIn
    didDisconnectWithUser:(GIDGoogleUser *)user
         withError:(NSError *)error {
      // Perform any operations when the user disconnects from app here.
      // ...
    }
    

Add the sign-in button

Next, you will add the Google Sign-In button so that the user can initiate the sign-in process. Make the following changes to the view controller that manages your app's sign-in screen:

  1. In the view controller's .h file, add the Google Sign-In interface and declare that this class implements the GIDSignInUIDelegate protocol.
    #import <Google/SignIn.h>
    
    @interface ViewController : UIViewController <GIDSignInUIDelegate>
    
  2. In the view controller, override the viewDidLoad method to set the UI delegate of the GIDSignIn object, and (optionally) to sign in silently when possible.
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      // TODO(developer) Configure the sign-in button look/feel
    
      [GIDSignIn sharedInstance].uiDelegate = self;
    
      // Uncomment to automatically sign in the user.
      //[[GIDSignIn sharedInstance] signInSilently];
    }
    
  3. In these examples, the view controller is a subclass of UIViewController. If, in your project, the class that implements GIDSignInUIDelegate is not a subclass of UIViewController, implement the signInWillDispatch:error:, signIn:presentViewController:, and signIn:dismissViewController: methods of the GIDSignInUIDelegate protocol. For example:
    // Implement these methods only if the GIDSignInUIDelegate is not a subclass of
    // UIViewController.
    
    // Stop the UIActivityIndicatorView animation that was started when the user
    // pressed the Sign In button
    - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
      [myActivityIndicator stopAnimating];
    }
    
    // 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];
    }
  4. Add a GIDSignInButton to your storyboard, XIB file, or instantiate it programmatically. To add the button to your storyboard or XIB file, add a View and set its custom class to GIDSignInButton.
  5. If you want to customize the button, do the following:
    1. In your view controller's .h file, declare the sign-in button as a property.
      @property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
    2. Connect the button to the signInButton property you just declared.
    3. Customize the button by setting the properties of the GIDSignInButton object.

Next, you can implement and handle the sign-out button.

Sign out the user

You can use the signOut method of the GIDSignIn object to sign out your user on the current device, for example:

- (IBAction)didTapSignOut:(id)sender {
  [[GIDSignIn sharedInstance] signOut];
}