Google Sign-In quick migration guide

If you have an existing iOS application using Google+ Sign-In, this guide shows you the bare minimum changes necessary to migrate your application to Google Sign-In.

1. Changes for the ViewController header file

  1. Add the Google Sign in 2.0 interface:

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

    @interface ViewController : UIViewController <GIDSignInDelegate, GIDSignInUIDelegate>
      // ... 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. If you only want to sign in users with their Google accounts, request the profile and email scopes. If you want to sign in users to make use of Google+ features, request the https://www.googleapis.com/auth/plus.login scope.

    @implementation ViewController
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      GIDSignIn* signIn = [GIDSignIn sharedInstance];
      if (self.fetchEmailToggle.isEnabled) {
        signIn.shouldFetchBasicProfile = YES;
      }
      signIn.clientID = kClientId;
      signIn.scopes = @[ @"profile", @"email" ];
      signIn.delegate = self;
      signIn.uiDelegate = 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 *)application openURL:(NSURL *)url
          sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
      return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication
          annotation:annotation];
    }
    

4. Add URL types

In your project's Info tab, add two URL types.

  • In one 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.
  • In the other URL type, specify a unique string in the Identifier field, and specify your app's bundle identifier in the URL Schemas field.