Integrating Google Sign-In into your iOS app

This page shows you how to integrate Google Sign-In into an iOS app based on UIKit and the UIApplicationDelegate lifecycle. If your app is scene-based or uses SwiftUI, you will need to adapt these instructions to your chosen lifecycle.

Before you begin

Download the dependencies and configure your Xcode project.

1. Configure the Google Sign-In library

  1. Create a GIDConfiguration object using the client IDs you created while completing the getting started steps. You can do this wherever you keep app configuration state, such as a singleton object.

    Swift

    let signInConfig = GIDConfiguration.init(clientID: "YOUR_IOS_CLIENT_ID")
    

    Objective-C

    signInConfig = [[GIDConfiguration alloc] initWithClientID:@"YOUR_IOS_CLIENT_ID"];
    

    If you need to get users' ID tokens for backend authentication, also set the serverClientID parameter. If you want to optimize the sign-in flow for a Google Workspace domain, or if you want to specify an OpenID realm, you can use the hostedDomain and openIDRealm parameters, respectively.

  2. In your AppDelegate’s application:openURL:options method, call GIDSignIn's handleURL: method:

    Swift

    func application(
      _ app: UIApplication,
      open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
      var handled: Bool
    
      handled = GIDSignIn.sharedInstance.handle(url)
      if handled {
        return true
      }
    
      // Handle other custom URL types.
    
      // If not handled by this app, return false.
      return false
    }
    

    Objective-C

    - (BOOL)application:(UIApplication *)app
                openURL:(NSURL *)url
                options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
      BOOL handled;
    
      handled = [GIDSignIn.sharedInstance handleURL:url];
      if (handled) {
        return YES;
      }
    
      // Handle other custom URL types.
    
      // If not handled by this app, return NO.
      return NO;
    }
    

2. Attempt to restore the user's sign-in state

When your app starts up, call restorePreviousSignInWithCallback to try and restore the sign-in state of users who already signed in using Google. Doing so ensures users don't have to sign in every time they open your app (unless they've signed out).

Apps often do this in the AppDelegate's application:didFinishLaunchingWithOptions: method, and use the result to determine which view to present to the user. For example:

Swift

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
  GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
    if error != nil || user == nil {
      // Show the app's signed-out state.
    } else {
      // Show the app's signed-in state.
    }
  }
  return true
}

Objective-C

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user,
                                                                NSError * _Nullable error) {
    if (error) {
      // Show the app's signed-out state.
    } else {
      // Show the app's signed-in state.
    }
  }];
  return YES;
}

3. Add a Google Sign-In button

  1. Add a "Sign in with Google" button to your sign-in View. You can use the GIDSignInButton class to automatically generate a button with Google branding (recommended) or create your own button with custom styling.

    To add a GIDSignInButton to a storyboard or XIB file, add a View and set its custom class to GIDSignInButton. Note that when you add a GIDSignInButton View to your storyboard, the sign-in button doesn't render in the interface builder. Run the app to see the sign-in button.

    You can customize the appearance of a GIDSignInButton by setting its colorScheme and style properties:

    GIDSignInButton style properties
    colorScheme kGIDSignInButtonColorSchemeLight
    kGIDSignInButtonColorSchemeDark
    style kGIDSignInButtonStyleStandard
    kGIDSignInButtonStyleWide
    kGIDSignInButtonStyleIconOnly
  2. Connect the button to a method in your ViewController that calls signInWithConfiguration:, passing the configuration object you created in step 1. For example, use an IBAction:

    Swift

    @IBAction func signIn(sender: Any) {
      GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
        guard error == nil else { return }
    
        // If sign in succeeded, display the app's main content View.
      }
    }
    

    Objective-C

    - (IBAction)signIn:(id)sender {
      [GIDSignIn.sharedInstance signInWithConfiguration:signInConfig
                               presentingViewController:self
                                               callback:^(GIDGoogleUser * _Nullable user,
                                                          NSError * _Nullable error) {
        if (error) {
          return;
        }
    
        // If sign in succeeded, display the app's main content View.
      }];
    }
    

4. Add a sign-out button

  1. Add a sign-out button to your app, visible to signed-in users.

  2. Connect the button to a method in your ViewController that calls signOut:. For example, use an IBAction:

    Swift

    @IBAction func signOut(sender: Any) {
      GIDSignIn.sharedInstance.signOut()
    }
    

    Objective-C

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

Next steps

Now that users can sign in to your app using their Google Accounts, learn how to: