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.
- In your app delegate's .h file, declare that this class implements the
GIDSignInDelegate
protocol.@import GoogleSignIn; @interface AppDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>
- In your app delegate's
application:didFinishLaunchingWithOptions:
method, configure theGIDSignIn
shared instance and set the sign-in delegate.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GIDSignIn sharedInstance].clientID = @"YOUR_CLIENT_ID"; [GIDSignIn sharedInstance].delegate = self; return YES; }
- Implement the
application:openURL:options:
method of your app delegate. The method should call thehandleURL
method of theGIDSignIn
instance, which will properly handle the URL that your application receives at the end of the authentication process.- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options { return [[GIDSignIn sharedInstance] handleURL:url]; }
For your app to run on iOS 8 and older, also implement the deprecated
application:openURL:sourceApplication:annotation:
method.- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[GIDSignIn sharedInstance] handleURL:url]; }
- 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 { if (error != nil) { if (error.code == kGIDSignInErrorCodeHasNoAuthInKeychain) { NSLog(@"The user has not signed in before or they have since signed out."); } else { NSLog(@"%@", error.localizedDescription); } return; } // 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:
- In the view controller, override the
viewDidLoad
method to set thepresentingViewController
of theGIDSignIn
object, and (optionally) to restore the previous sign-in state when possible.- (void)viewDidLoad { [super viewDidLoad]; [GIDSignIn sharedInstance].presentingViewController = self; // Automatically sign in the user. [[GIDSignIn sharedInstance] restorePreviousSignIn]; }
- 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 toGIDSignInButton
. - If you want to customize the button, do the following:
- In your view controller's .h file, declare the sign-in button as a
property.
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
- Connect the button to the
signInButton
property you just declared. - Customize the button by setting the properties of the GIDSignInButton object.
- In your view controller's .h file, declare the sign-in button as a
property.
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]; }