Google+ Platform for iOS

Manage app activities in Google

After the user signs in using the Google+ Sign-In button, you can write app activities to Google on behalf of your user with the moments methods. App activities reflect a variety of user actions including check-ins, reviews, commenting on articles, and more. Users control the visibility of their app activities on Google using circles. Your app can use the type of activity that best reflects your content or the user's action.

Before you begin

You must create and register an APIs Console project, download the SDK, and initialize the Google+ client as instructed in Getting Started with iOS.

Write an app activity to Google

When a user performs an action within your app, your app can write an app activity to the authenticated user's Google account. This example shows you how to do that by calling the queryForMomentsInsertWithObject:moment method from within your iOS app.

  1. Implement Sign in with Google+.

  2. In addition to the sign-in code in the previous step, you'll need to list the app activity types your app will write. After defining the [GPPSignIn sharedInstance].clientID and [GPPSignIn sharedInstance].scope values, define the actions your app will write as an NSArray. For example:

    [GPPSignIn sharedInstance].actions = [NSArray arrayWithObjects:
          @"http://schemas.google.com/AddActivity",
          @"http://schemas.google.com/BuyActivity",
          @"http://schemas.google.com/CheckInActivity",
          @"http://schemas.google.com/CommentActivity",
          @"http://schemas.google.com/CreateActivity",
          @"http://schemas.google.com/ListenActivity",
          @"http://schemas.google.com/ReserveActivity",
          @"http://schemas.google.com/ReviewActivity",
    nil];
    
  3. In your app's view controller, add the class directive.

    @class GTLServicePlus;
    
  4. Import the open source headers.

    #import <GoogleOpenSource/GoogleOpenSource.h>
    
  5. Create a GTLServicePlus instance.

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

    [plusService setAuthorizer:static_auth];
    
  7. Create a moment.

    GTLPlusMoment *moment = [[[GTLPlusMoment alloc] init] autorelease];
    
    // You can replace AddActivity with another valid Google+ app activity type.
    // See: https://developers.google.com/+/mobile/ios/api/moment-types
    moment.type = [NSMutableString @"http://schemas.google.com/AddActivity"];
    
    GTLPlusItemScope *target = [[[GTLPlusItemScope alloc] init] autorelease];
    GTLPlusItemScope *result = [[[GTLPlusItemScope alloc] init] autorelease];
    target.url = @"https://example.com";
    
    result.url = @"https://example.com";
    
    moment.target = target;
    moment.result = result; // Optional in many cases, depending on moment type.
    
  8. Write the moment.

    GTLQueryPlus *query =
          [GTLQueryPlus queryForMomentsInsertWithObject:moment
                                                 userId:@"me"
                                             collection:kGTLPlusCollectionVault];
    
      [plusService executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket,
                            id object,
                            NSError *error) {
          if (error) {
            GTMLoggerError(@"Got bad response from "
                           @"plus.moments.insert: %@",
                           error);
    
          } else {
            self.momentStatus.text =
              [NSMutableString stringWithFormat:@"Moment Sent: %@",
                                                moment.type];
          }
      }];
    

The queryForMomentsInsertWithObject method is a native API binding around the moments.insert method.

Google may obtain an image by crawling your target URL for moments that do not specify an image field. All content associated with your target URL that Google obtains and uses in connection with a moment is considered content that you submit to Google under Section 5 of the Google APIs Terms of Service.

List app activities

Retrieve app activities written by your app using the GTLQuery object. The executeQuery function will call the completion block, and will return a GTLPlusPeopleFeed. You can retrieve an NSArray of GTLPlusPerson objects from the people feed with the items property—remember to call retain on it to ensure it is not released prematurely. If there is a problem retrieving the feed, the NSError *error variable on the completion block will be set.

  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 moments.

    GTLQueryPlus *query =
        [GTLQueryPlus queryForMomentsListWithUserId:@"me"
                                         collection:kGTLPlusCollectionVault];
    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                                id object,
                                NSError *error) {
                [momentsData_ autorelease];
                if (error) {
                  GTMLoggerError(@"Status: Error: %@", error);
                } else {
                  GTLPlusMomentsFeed *moments = (GTLPlusMomentsFeed *)object;
                 NSArray momentsData  = [moments.items retain];
                }
            }];
    

The queryForMomentsListWithUserId method is a native API binding around the moments.list method.

Remove an app activity from Google

Use the queryForMomentsRemoveWithIdentifier method on the GTLQueryPlus object to remove a moment from Google on behalf of the signed-in user. This method requires that you pass an identifier for the moment to be removed, which is available by calling Moment.getId().

The following example of deleting an app activity assumes you have a GTLPlusMoment *moment variable.

  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 and set up the delete call. The moment is of type GTLPlusMoment *.

    GTLQueryPlus *query =
        [GTLQueryPlus queryForMomentsRemoveWithIdentifier:moment.identifier];
    
  4. Execute the query. If there is an error, the error object will be non-null, otherwise the call completed successfully.

    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                                id object,
                                NSError *error) {
                if (error) {
                    GTMLoggerError(@"Status: Error: %@", error);
                } else {
                    NSString *status = [NSString stringWithFormat:@"Deleted %@",
                                           moment.identifier];
                }
            }];
    

The queryForMomentsRemoveWithIdentifier method is a native API binding around the moments.remove method.

Next steps

Authentication required

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

Signing you in...

Google Developers needs your permission to do that.