Implementing Sign-in in Your iOS Game

This guide will give you a brief introduction to how to use the GPGManager to sign the user in to Google Play games services.

Authentication and sign in

The GPGManager works with the GIDSignIn class (part of the Google Sign-in SDK) to perform most of the work required to sign the user in and out from Google Play games services. As a general rule, we recommend using the GPGManager instead of the GIDSignIn class to perform sign-in, as the process tends to be simpler.

There are three methods you can use from the [GPGManager sharedInstance] object to sign the user in:

  • The signInWithClientID:silently: method is the most common way to sign the user in. The clientID is an NSString that represents the clientID of your project that you created within the developer console.
  • The signInWithClientID:silently:withExtraScopes method is the same as above, but it allows you to requests additional scopes, as an array of NSStrings, when signing in.
  • The signIn method should only be used if you choose to use GIDSignIn to sign the user in. You should call this method after the GIDSignIn object has finished signing the user in, which you can detect in the GIDSignInDelegate's signIn:didSignInForUser:withError method. For more information on this process, see Google Sign-in for iOS.

When silently is set to YES, the GPGManager will attempt to sign the user in silently in the background. This will succeed if the user has signed in to your game in the past, has not signed out in the meantime, and you are not asking for any additional scopes. The first two methods will return YES immediately if it believes it can sign the user in successfully, although this does not mean the user has signed in yet. You need to capture those events in the GPGStatusDelegate, as detailed below.

A typical sign in flow is...

  1. Attempt to sign the user in using signInWithClientID with silently set to YES.
  2. If the call to the silent sign-in method returns NO, show the user a sign in button.
  3. If the user clicks the sign-in button, call sign-in again using signInWithClientID with silently set to NO.

Please refer to the Getting Started guide for a step-by-step guide towards implementing this flow in your app.

Testing for user sign-in

You can use the GPGManager's signedIn property to determine if the user has been signed in to Google Play games services. This can be useful if you are creating a game where sign-in is optional:

- (void)gameOver
{
  if ([GPGManager sharedInstance].signedIn) {
    // Submit score to leaderboard!
  } else {
    // Display "Sign in with Google to use leaderboards!" message
  }
}

The GPGManager also has a hasAuthorizer property, which can indicate that the user has received an auth token from the server, but it does not necessarily mean they have completed signing in to Google Play games services. Use the signedIn property for that.

Adding support for Saved Games

The Saved Games service allows you to save player game data to the cloud. To add Saved Games (also known as "Snapshots") to your game, set the snapshotsEnabled property in your GPGManager to YES before you start the sign-in process.

[GPGManager sharedInstance].snapshotsEnabled = YES;
[GPGManager sharedInstance].statusDelegate = self;
_tryingSilent  = [[GPGManager sharedInstance] signInWithClientID:kClientID silently:YES];

Signing the user out

You can use the GPGManager's signOut method to sign the user out. This signs out the player from Google Play games services and removes any cached data for the user.

All of the built-in UI provided by the iOS SDK includes a settings panel where the user can sign-out as well, so if you are using the built-in UI, adding an explicit sign-out button is optional.

The GPGStatusDelegate

The GPGManager uses the GPGStatusDelegate protocol to report when the user has successfully completed signing in, or signing out, to Google Play games services.

The two most common delegate methods you will use are didFinishGamesSignInWithError and didFinishGamesSignOutWithError. The first method indicates the user has fully completed the sign in process and it is okay to start making calls to Google Play games services, while the second method indicates the user has signed out from Google Play games services.

Note that the didFinishGoogleAuthWithError method indicates that the user has successfully retrieved an auth token from the server, but it does not mean the user has completed signing in to game services. (The GPGManager class needs to retrieve some additional game-related information before the user is completely signed in to Google Play games services.) In addition, this method will not be called if you are using the signIn method.

The didFinishGamesSignOutWithError method will be called whenever the user signs out, either using a signOut call (most likely initiated from a button) or by signing out through the settings panel from the built-in UI. As such, you need to be aware that this method may be called from a different ViewController than the one where you originally placed your sign-out button.

For a full list of GPGStatusDelegate methods, please refer to the API Documentation.

Enviar comentarios sobre…

Play Games Services for iOS
Play Games Services for iOS