iOS Quickstart

The steps described on this page explain how to quickly create a simple iOS application that makes requests to the YouTube Data API. This sample shows how to retrieve data about the GoogleDevelopers YouTube channel. The code also includes comments that explain how to modify the query to retrieve data about the current user's YouTube channel.

Prerequisites

To run this quickstart, you'll need:

  • Xcode 8.0 or greater.
  • CocoaPods dependency manager.
  • Access to the internet and a web browser.
  • A Google account.

Step 1: Turn on the YouTube Data API

  1. Use this wizard to create a new app or select an existing one.
  2. Enter an app name in the App name field, and enter "com.example.QuickstartApp" into the iOS Bundle ID field.
  3. Click the Continue button to proceed to choose and configure services.
  4. Click the Google Sign-In icon. (It might already be selected.) Then click the Enable Google Sign-in button.
  5. Click the Continue button to proceed to generate configuration files.
  6. Click the Download GoogleService-Info.plist button to download the configuration file. Take note of where you saved it.
  7. Click the Enable the YouTube Data API button.
  8. Use the dropdown menu to select the project that has the same name as the app name that you specified in step 2. Then click the Continue button.
  9. Click the Go to credentials button. This actually takes you to a page to set up credentials, which is not what you need to do, so click the Cancel button. (The wizard has already created credentials for you.)
  10. The credentials page shows API keys and OAuth 2.0 client IDs created for your project. The OAuth 2.0 client IDs section lists two IDs, one for a web application and one for iOS. Click on the link for the iOS client.
  11. Open the GoogleService-Info.plist file that you downloaded in step f. Compare the CLIENT_ID and REVERSED_CLIENT_ID values in the file with the Client ID and iOS URL scheme shown for the iOS OAuth 2.0 client. If they do not match, update the values in the file to those shown for the iOS client.

Step 2: Prepare the workspace

  1. Open Xcode and create a new project:
    1. Click File > New > Project, select the iOS > Application > Single View Application template, and click Next.
    2. Set the Product Name to "QuickstartApp", Organization Identifier to "com.example", and Language to Objective-C. Below the organization identifer, you should see a generated Bundle Identifier that matches the iOS Bundle ID (com.example.QuickstartApp) that you entered in step 1.b.
    3. Click Next.
    4. Select a destination directory for the project and click Create.
  2. Close the project by clicking File > Close Project.
  3. Open a Terminal window and navigate to the directory that contains the QuickstartApp.xcodeproj file you just created.
  4. Run the following commands to create the Podfile, install the library, and open the resulting XCode project:

    cat << EOF > Podfile &&
    platform :ios, '8.0'
    target 'QuickstartApp' do
        pod 'GoogleAPIClientForREST/YouTube', '~> 1.2.1'
        pod 'Google/SignIn', '~> 3.0.3'
    end
    EOF
    pod install &&
    open QuickstartApp.xcworkspace
    

  5. In the XCode Project Navigator select the project node "QuickstartApp". Then click the menu item File > Add files to "QuickstartApp".

  6. Locate the GoogleService-Info.plist file downloaded earlier and select it. Click the Options button.

  7. Make the following selections in the options window and then click the Add button:

    1. Check the Copy items if needed checkbox.
    2. Check all targets listed in the Add to targets section.

  8. With the project node still selected, select "QuickstartApp" in the TARGETS section as shown in the two images below:

    1. Click the area shown in this screenshot:

    2. Then select the proper target:

  9. Select the Info tab, and expand the URL Types section.

  10. Click the + button, and add a URL scheme for your reversed client ID. To find this value, open the GoogleService-Info.plist configuration file that you selected in step 2.f. Look for the REVERSED_CLIENT_ID key. Copy the value of that key, and paste it into the URL Schemes box on the configuration page. Leave the other fields blank.

  11. Rebuild the project:

    1. Click Product > Clean Build Folder (while holding the option key).
    2. Click Product > Build.

Step 3: Set up the sample

Replace the contents of the following files with the code provided:

AppDelegate.h
#import <UIKit/UIKit.h>
#import <Google/SignIn.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
AppDelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSError* configureError;
    [[GGLContext sharedInstance] configureWithError: &configureError];
    NSAssert(!configureError, @"Error configuring Google services: %@", configureError);

    return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:sourceApplication
                                      annotation:annotation];
}


@end
ViewController.h
#import <UIKit/UIKit.h>
#import <Google/SignIn.h>
#import <GTLRYouTube.h>

@interface ViewController : UIViewController <GIDSignInDelegate, GIDSignInUIDelegate>

@property (nonatomic, strong) IBOutlet GIDSignInButton *signInButton;
@property (nonatomic, strong) UITextView *output;
@property (nonatomic, strong) GTLRYouTubeService *service;


@end
ViewController.m
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Configure Google Sign-in.
    GIDSignIn* signIn = [GIDSignIn sharedInstance];
    signIn.delegate = self;
    signIn.uiDelegate = self;
    signIn.scopes = [NSArray arrayWithObjects:kGTLRAuthScopeYouTubeReadonly, nil];
    [signIn signInSilently];

    // Add the sign-in button.
    self.signInButton = [[GIDSignInButton alloc] init];
    [self.view addSubview:self.signInButton];

    // Create a UITextView to display output.
    self.output = [[UITextView alloc] initWithFrame:self.view.bounds];
    self.output.editable = false;
    self.output.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
    self.output.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.output.hidden = true;
    [self.view addSubview:self.output];

    // Initialize the service object.
    self.service = [[GTLRYouTubeService alloc] init];
}

- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
    if (error != nil) {
        [self showAlert:@"Authentication Error" message:error.localizedDescription];
        self.service.authorizer = nil;
    } else {
        self.signInButton.hidden = true;
        self.output.hidden = false;
        self.service.authorizer = user.authentication.fetcherAuthorizer;
        [self fetchChannelResource];
    }
}


// Construct a query and retrieve the channel resource for the GoogleDevelopers
// YouTube channel. Display the channel title, description, and view count.
- (void)fetchChannelResource {
    GTLRYouTubeQuery_ChannelsList *query =
    [GTLRYouTubeQuery_ChannelsList queryWithPart:@"snippet,statistics"];
  query.identifier = @"UC_x5XG1OV2P6uZZ5FSM9Ttw";
  // To retrieve data for the current user's channel, comment out the previous
  // line (query.identifier ...) and uncomment the next line (query.mine ...).
  // query.mine = true;

  [self.service executeQuery:query
                    delegate:self
           didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}

// Process the response and display output
- (void)displayResultWithTicket:(GTLRServiceTicket *)ticket
             finishedWithObject:(GTLRYouTube_ChannelListResponse *)channels
                          error:(NSError *)error {
  if (error == nil) {
    NSMutableString *output = [[NSMutableString alloc] init];
    if (channels.items.count > 0) {
      [output appendString:@"Channel information:\n"];
      for (GTLRYouTube_Channel *channel in channels) {
        NSString *title = channel.snippet.title;
        NSString *description = channel.snippet.description;
        NSNumber *viewCount = channel.statistics.viewCount;
        [output appendFormat:@"Title: %@\nDescription: %@\nViewCount: %@\n", title, description, viewCount];
      }
    } else {
      [output appendString:@"Channel not found."];
    }
    self.output.text = output;
  } else {
    [self showAlert:@"Error" message:error.localizedDescription];
  }
}



// Helper for showing an alert
- (void)showAlert:(NSString *)title message:(NSString *)message {
    UIAlertController *alert =
    [UIAlertController alertControllerWithTitle:title
                                        message:message
                                 preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok =
    [UIAlertAction actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
     {
         [alert dismissViewControllerAnimated:YES completion:nil];
     }];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];
}


@end

Step 4: Run the sample

Switch to the QuickstartApp scheme by clicking Product > Scheme > QuickstartApp and run the sample (Cmd+R) using the device simulator or a configured device. The first time you run the sample, it will prompt you to log in to your Google account and authorize access.

Notes

  • Authorization information is stored in your Keychain, so subsequent executions will not prompt for authorization.

Further reading

Enviar comentarios sobre…