Google+ Platform for iOS

Getting people and profile information

After you have signed in a user with Google, you can access the user's age range, language, public profile information, and people that they have circled. If you request the plus.profile.emails.read scope, you can also get their email address. With this rich social data, you can build engaging experiences and an instant community in your app. For example, you might connect your user with their friends that also use your app or you might make suggestions based on their friends' activities within your app.

Before you begin

Create and register an APIs Console project, install the SDK, and initialize the Google+ client as instructed in Getting Started with iOS.

Retrieve a collection of people

Use the queryForPeopleListWithUserId:collection: method on the GTLQueryPlus object to create a query for people in the specified collection. This method returns a GTLQueryPlus object that you can pass to the executeQuery method of GTLPlusService to trigger an asynchronous request. The results will be handled by a completion-handler block defined during the executeQuery call.

The following example uses the visible (kGTLPlusCollectionVisible) collection to obtain a list of people who the signed-in user has added to one or more circles, which is limited to the circles the user made visible to the requesting app. This list does not include names of circles.

  1. Create a GTLServicePlus instance to send a request to Google+.

    GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease];
    plusService.retryEnabled = YES;
    
  2. Set a valid GTMOAuth2Authentication object as the authorizer.

    [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
    
  3. Create a GTLQuery object to list people who are visible to this sample app.

    GTLQueryPlus *query =
        [GTLQueryPlus queryForPeopleListWithUserId:@"me"
                                        collection:kGTLPlusCollectionVisible];
    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                                GTLPlusPeopleFeed *peopleFeed,
                                NSError *error) {
                if (error) {
                  GTMLoggerError(@"Error: %@", error);
                } else {
                  // Get an array of people from GTLPlusPeopleFeed
                  NSArray* peopleList = [peopleFeed.items retain];
                }
            }];
    

The queryForPeopleListWithUserId:collection: method on the GTLQueryPlus object is a native API binding around the people.list API.

Retrieve profile information

Use the queryForPeopleGetWithUserId method to request profile information about a specific person on Google+ by passing in their user ID. The special value "me" can be used as the ID to indicate the currently signed-in user, as shown in the following example.

  1. Create a GTLServicePlus instance to send a request to Google+.

    GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease];
    plusService.retryEnabled = YES;
    
  2. Set a valid GTMOAuth2Authentication object as the authorizer.

    [plusService setAuthorizer:appDelegate.authentication];
    
  3. Create a GTLQuery object to get the details of the user with the given user ID. The special value "me" indicates the currently signed in user, but you could use any other valid user ID. Returns a GTLPlusPerson.

    GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
    
    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                                GTLPlusPerson *person,
                                NSError *error) {
                if (error) {
                    GTMLoggerError(@"Error: %@", error);
                } else {
                    // Retrieve the display name and "about me" text
                    [person retain];
                    NSString *description = [NSString stringWithFormat:
                                            @"%@\n%@", person.displayName,
                                             person.aboutMe];
                 }
            }];
    

The queryForPeopleGetWithUserId method on the GTLQueryPlus object is a native API binding around the people.get API.

Retrieve an authenticated user's email address

Set the shouldFetchGooglerUserEmail property to YES on your sign-in object. You can then access the user's email from the userEmail property on the authentication object after the user signs in.

    - (void)viewDidLoad {
      [super viewDidLoad];

      AppDelegate *appDelegate = (AppDelegate *)
          [[UIApplication sharedApplication] delegate];

      GPPSignIn *signIn = [GPPSignIn sharedInstance];
      signIn.shouldFetchGoogleUserEmail = YES;
      signIn.delegate = self;

    }

    - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                       error: (NSError *)error {
      if(!error) {
        // Get the email address.
        NSLog(@"%@", signIn.authentication.userEmail);
      }
    }

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.