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 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 (AppDelegate.swift), declare that this class implements the GIDSignInDelegate protocol.
    import GoogleSignIn
    
    ...
    
    class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
    
    
  2. In your app delegate's application:didFinishLaunchingWithOptions: method, configure the GIDSignIn shared instance and set the sign-in delegate.
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Initialize sign-in
      GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
      GIDSignIn.sharedInstance().delegate = self
    
      return true
    }
    
  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.
    @available(iOS 9.0, *)
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
      return GIDSignIn.sharedInstance().handle(url)
    }
    

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

    func application(_ application: UIApplication,
                     open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
      return GIDSignIn.sharedInstance().handle(url)
    }
    
  4. In the app delegate, implement the GIDSignInDelegate protocol to handle the sign-in process by defining the following methods:
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
      if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
          print("The user has not signed in before or they have since signed out.")
        } else {
          print("\(error.localizedDescription)")
        }
        return
      }
      // 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
      // ...
    }
    
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
              withError error: 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, override the viewDidLoad method to set the presentingViewController of the GIDSignIn object, and (optionally) to restore the previous sign-in state when possible.
    override func viewDidLoad() {
      super.viewDidLoad()
    
      GIDSignIn.sharedInstance()?.presentingViewController = self
    
      // Automatically sign in the user.
      GIDSignIn.sharedInstance()?.restorePreviousSignIn()
    
      // ...
    }
  2. 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.
  3. If you want to customize the button, do the following:
    1. In your view controller, declare the sign-in button as a property.
      @IBOutlet weak var signInButton: GIDSignInButton!
    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 func didTapSignOut(_ sender: AnyObject) {
  GIDSignIn.sharedInstance().signOut()
}