Google Sign-In quick migration guide

Migrating from versions prior to v5.0.0

To migrate your app from a version of the Google Sign-in SDK prior to v5.0.0, make the following changes:

  • Handle the removal of the GIDSignInUIDelegate protocol in SDK v5.0.0:

    • Remove any declarations of conformance to the GIDSignInUIDelegate protocol:

      Swift

      // Old:
      class ViewController: UIViewController, GIDSignInUIDelegate
      
      // New:
      class ViewController: UIViewController
      

      Objective-C

      // Old:
      @interface ViewController : UIViewController <GIDSignInUIDelegate>
      
      // New:
      @interface ViewController : UIViewController
      
    • If you implemented the GIDSignInUIDelegate protocol's signInWillDispatch:error:, signIn:presentViewController: or signIn:dismissViewController: methods, you can probably delete them, but be sure to retain any logic not related to presenting the SFSafariViewController.

    • Instead of setting the GIDSignIn object's uiDelegate property to an object that conforms to the GIDSignInUIDelegate protocol, set its presentingViewController property to the view controller that contains the Google Sign-in button:

      Swift

      // Old:
      GIDSignIn.sharedInstance()?.uiDelegate = self
      
      // New:
      GIDSignIn.sharedInstance()?.presentingViewController = self
      

      Objective-C

      // Old:
      [GIDSignIn sharedInstance].uiDelegate = self;
      
      // New:
      [GIDSignIn sharedInstance].presentingViewController = self;
      
  • Stop passing sourceApplication and annotation to handleURL:

    Swift

    // Old:
    GIDSignIn.sharedInstance().handle(url,
        sourceApplication: sourceApplication,
        annotation: annotation)
    
    // New:
    GIDSignIn.sharedInstance().handle(url)
    

    Objective-C

    // Old:
    [[GIDSignIn sharedInstance] handleURL:url
                        sourceApplication:sourceApplication
                               annotation:annotation];
    
    // New:
    [[GIDSignIn sharedInstance] handleURL:url];
    
  • Update calls to signInSilently and hasAuthInKeychain to restorePreviousSignIn and hasPreviousSignIn.

    Note that hasPreviousSignIn doesn't check the saved credential's client ID and granted scopes, so if it's possible that credentials were saved with different values—for example, if you ever used a different client ID or requested a different set of scopes—you should also confirm these values are what you expect before using the restored credentials.

    Swift

    // Old:
    guard let signIn = GIDSignIn.sharedInstance() else { return }
    if (signIn.hasAuthInKeychain()) {
      signIn.signInSilently()
    }
    
    // New:
    guard let signIn = GIDSignIn.sharedInstance() else { return }
    if (signIn.hasPreviousSignIn()) {
      signIn.restorePreviousSignIn()
    
      // If you ever changed the client ID you use for Google Sign-in, or
      // requested a different set of scopes, then also confirm that they
      // have the values you expect before proceeding.
      if (signIn.currentUser.authentication.clientID != YOUR_CLIENT_ID
          // TODO: Implement hasYourRequiredScopes
          || !hasYourRequiredScopes(signIn.currentUser.grantedScopes)) {
        signIn.signOut()
      }
    }
    

    Objective-C

    // Old:
    GIDSignIn *signIn = [GIDSignIn sharedInstance];
    if ([signIn hasAuthInKeychain]) {
      [signIn signInSilently];
    }
    
    // New:
    GIDSignIn *signIn = [GIDSignIn sharedInstance];
    if ([signIn hasPreviousSignIn]) {
      [signIn restorePreviousSignIn];
    
      // If you ever changed the client ID you use for Google Sign-in, or
      // requested a different set of scopes, then also confirm that they
      // have the values you expect before proceeding.
      if (signIn.currentUser.authentication.clientID != YOUR_CLIENT_ID
          // TODO: Implement hasYourRequiredScopes
          || !hasYourRequiredScopes(signIn.currentUser.grantedScopes)) {
        [signIn signOut];
      }
    }
    
  • Remove calls to getAccessTokenWithHandler: and refreshAccessTokenWithHandler:; instead, use getTokensWithHandler: and refreshTokensWithHandler:.

    Swift

    // Old:
    guard let signIn = GIDSignIn.sharedInstance() else { return }
    signIn.currentUser.authentication.getAccessToken { accessToken, error ->
      guard error == nil else { return }
      let token = accessToken
    }
    signIn.currentUser.authentication.refreshAccessToken { accessToken, error ->
      guard error == nil else { return }
      let token = accessToken
    }
    
    // New:
    guard let signIn = GIDSignIn.sharedInstance() else { return }
    signIn.currentUser.authentication.getTokens { auth, error ->
      guard error == nil else { return }
      let token = auth.accessToken  // You can also get idToken and refreshToken.
    }
    signIn.currentUser.authentication.refreshTokens { auth, error ->
      guard error == nil else { return }
      let token = auth.accessToken  // You can also get idToken and refreshToken.
    }
    

    Objective-C

    // Old:
    GIDSignIn *signIn = [GIDSignIn sharedInstance];
    [signIn.currentUser.authentication
     getAccessTokenWithHandler:^(NSString *accessToken, NSError *error) {
      if (error != nil) { return; }
      NSString *token = accessToken;
    }];
    [signIn.currentUser.authentication
     refreshAccessTokenWithHandler:^(NSString *accessToken, NSError *error) {
      if (error != nil) { return; }
      NSString *token = accessToken;
    }];
    
    // New:
    GIDSignIn *signIn = [GIDSignIn sharedInstance];
    [signIn.currentUser.authentication
     getTokensWithHandler:^(GIDAuthentication *auth, NSError *error) {
      if (error != nil) { return; }
      NSString *token = auth.accessToken;  // You can also get idToken and refreshToken.
    }];
    [signIn.currentUser.authentication
     refreshTokensWithHandler:^(GIDAuthentication *auth, NSError *error) {
      if (error != nil) { return; }
      NSString *token = auth.accessToken;  // You can also get idToken and refreshToken.
    }];
    
  • If you access GIDGoogleUser.accessibleScopes at any point, use GIDGoogleUser.grantedScopes instead. Note that accessibleScopes contained the requested scopes, not the granted scopes, so in rare cases, some logic changes might also be necessary.

Migrating from Google+

To migrate from Google+, update your use of Google branding and scopes, and then migrate to the current SDK.

Migrating from Google+ branding and scopes

To migrate your app from Google+ Sign-In to Google Sign-In, you’ll need to update your sign in button, the scopes you request from the user and how to retrieve profile information from Google. Follow our Google Sign-In for iOS documentation for full instructions.

When updating your sign in button, do not refer to Google+ or use the color red. See the updated branding guidelines.

Most G+ Sign In applications requested some combination of the scopes: plus.login, plus.me and plus.profile.emails.read. By setting shouldFetchBasicProfile to YES (the default) you will automatically request the profile and email scopes. These scopes will provide the user’s name, profile picture, and email.

Migrating from the Google+ Sign-In SDK

If you have an existing iOS application using the Google+ Sign-In SDK (GPPSignIn), this section shows you the bare minimum changes necessary to migrate your application to the Google Sign-In SDK (GIDSignIn).

1. Changes for the ViewController header file

  1. Add the Google Sign-In interface:

    #import <GoogleSignIn/GoogleSignIn.h>
    
  2. Make the class a delegate to receive Sign-In messages:

    @interface ViewController : UIViewController <GIDSignInDelegate>
      // ... Typically, you'll add buttons here
    @end
    

2. Changes for the ViewController implementation file

  1. Import Google sources:

    #import <GoogleSignIn/GoogleSignIn.h>
    
  2. Initialize the GIDSignIn shared instance. Request the profile scope.

    @implementation ViewController
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      GIDSignIn* signIn = [GIDSignIn sharedInstance];
      if (self.fetchEmailToggle.isEnabled) {
        signIn.shouldFetchBasicProfile = YES;
      } else {
        signIn.shouldFetchBasicProfile = NO;
      }
      signIn.clientID = kClientId;
      signIn.scopes = @[ @"profile" ];
      signIn.delegate = self;
      signIn.presentingViewController = self;
      self.statusField.text = @"Initialized auth2...";
    }
    
  3. Implement the delegate methods:

    - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
        // Perform any operations on signed in user here.
      self.statusField.text = @"Signed in user";
    }
    - (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error {
        // Perform any operations when the user disconnects from app here.
        self.statusField.text = @"Disconnected user";
    }
    
  4. Map buttons to Auth operations:

    - (IBAction)signInClicked:(id)sender {
      self.statusField.text = @"Clicked sign in!";
      [[GIDSignIn sharedInstance] signIn];
    }
    - (IBAction)signOutClicked:(id)sender {
      self.statusField.text = @"Clicked sign out!";
      [[GIDSignIn sharedInstance] signOut];
    }
    - (IBAction)disconnectClicked:(id)sender {
      self.statusField.text = @"Clicked disconnect!";
      [[GIDSignIn sharedInstance] disconnect];
    }
    - (IBAction)emailToggled:(id)sender {
      if (self.fetchEmailToggle.isEnabled) {
        [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES;
      } else {
        [GIDSignIn sharedInstance].shouldFetchBasicProfile = NO;
      }
    }
    

3. Changes for the AppDelegate implementation file

  1. Import GoogleSignIn:

    #import <GoogleSignIn/GoogleSignIn.h>
    
  2. Handle the URL from OAuth responses:

    - (BOOL)application:(UIApplication *)app
                openURL:(NSURL *)url
                options:(NSDictionary<NSString *, id> *)options {
      return [[GIDSignIn sharedInstance] handleURL:url];
    }
    
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
      return [[GIDSignIn sharedInstance] handleURL:url];
    }
    

4. Add a URL type

In your project's Info tab, add a URL type. Specify a unique string in the Identifier field, and specify your client ID in reversed order in the URL Schemas field. For example, if your client ID for iOS is CLIENT_ID_CODE.apps.googleusercontent.com, then specify com.googleusercontent.apps.CLIENT_ID_CODE in the URL Schemas field.