YouTube iOS Framework

It’s almost 2015 and while we are close to the hoverboard dream, but integrating YouTube API still seems to be stuck in 2012. Starting iOS 8, we have full(almost) support for Frameworks and I think a service like YouTube is a perfect fit. Although Google seems to be taking its own sweet time to release one, here is something you can work with in the mean time.

It’s a Framework which bundles the libGTLTouchStaticLib.a provided by google-data-api along with the resources, framework and other files and settings required.

Frameworks are only available on iOS 8. If you are targeting a previous versions this link will be helpful.

Also, this is still a work in progress so there are a few limitations

  • This framework targets an actual device, and will not work on simulator. I am trying to generate a universal Framework but the GTL project form Google just does not give me a universal binary… very frustrating :(
  • I was not able to bundle the required .xib file as a part of the framework, so you will need to add that manually.

First you need to set up your app on Google Developer Console. If you need help regarding this, follow the Step 1 here.

Next download the Framework zip from this link or from GitHub. Inside this zip you will find:

  1. YouToobAPI.framework: This is the framework which contains all the google data API code. It also contains a helper class, YouTubeHelper, written by me to work with the google API. To take a look at the class and its implementation you can download the files here.
  2. GTMOAuth2ViewTouch.xib: This xib is a part of the google API. Ideally this should have been a part of the Framework, but at the moment I just could not find a way to do that.

Ok so now we have everything we need, lets test this out.

Create a new project in Xcode. Lets go for a simple Single View Application template.

1. New Project

Next, create a group for the framework and xib in you project and drag-drop these files into this group. Make sure you select the ‘copy if needed’ checkbox.

2. Add files

Go you your project settings and select the target. In the general tab, you will see our framework listed under the ‘Linked Frameworks and Libraries’ section. Thats good, but we also need to tell Xcode to treat it as an Embedded Binary. So click the ‘+’ button under ‘Embedded Binaries’ and select the framework.

3. Add as embedded binary

4. Added embedded binary

Try a build now, if all went well we should not have any errors. This is about it. Our project is ready to start talking to YouTube service. Currently the only usable class exposed by our Framework is YouTubeHelper. But for the sake of this tutorial and most common tasks, that is sufficient.

Go to the view controller .h file and

#import <YouToobAPI/YouToobAPI.h>

This will also expose the YouTubeHelper class to your view, you don’t have to import it separately.

Also make your view controller conform to the YouTubeHelperDelegate protocol.

@interface ViewController : UIViewController

Next we need to implement the protocol methods and start taking to YouTube service.

In your view controller, .m, create a property for YouTubeHelper.

#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) YouTubeHelper * youtubeHelper;
@end
@implementation ViewController
...

We can initialise this property in viewDidLoad:

self.youtubeHelper = [[YouTubeHelper alloc] initWithDelegate:self];

Next we need to implement all the required delegates:

- (NSString *)youtubeAPIClientID {
    return @"YOUR_CLIENT_ID";
}

- (NSString *)youtubeAPIClientSecret {
    return @"YOUR_CLIENT_SECRET";
}

- (void)showAuthenticationViewController:(UIViewController *)authView {
    [self.navigationController presentViewController:authView animated:NO completion:nil];
}

- (void)authenticationEndedWithError:(NSError *)error {
    if (error) {
        NSLog(@"Auth failed with error: %@", error.description);
    }
}

Ok, we are all set up. From here you can start calling the methods in YouTubeHelper.

The first thing you will need to do is authenticate. Once that is done, you can be sure your app is communicating successfully with the API. Add a button to your view controller and in its selector call:

[_youTubeHelper authenticate];

You will notice that we are presenting the auth view though navigationController. Either you can change that or since we are using the ‘Single View Application’ template, we will need to embed our view controller in a  navigation controller. Open the storyboard, select your view controller.

5. Embed in navigation

Go to Editor > Embed In > Navigation Controller. This should make your view controller a child of a navigation controller.

Screen Shot 2014-11-18 at 1.01.32 pm

Ok now go ahead the test the authenticate function. You should be presented with a Google Sing In view.

For more functions of the YouTubeHelper class, like uploading a video, follow Step 4 here.

Advertisements

6 thoughts on “YouTube iOS Framework

  1. […] If you are only targeting iOS 8, I have created a Framework bundle to make things […]

  2. todopole says:

    Great Job! Really useful for me :) I’m going to use it right now!!

  3. Hi Nirbhay! Thanks for all the instructions on YT iOS support… actually I wasn’t able to test the framework because only supports arm64… is there any way I could make it available for armv7s too (iPhone 5)? thanks

  4. ViewController “const__strong” parameter incompatible

    Please, who resolved ??

  5. khursheed says:

    when u get logged into gmail account they give a specific code saying this that paste this code into your application now where exactly should i post this code.
    i mean is this the keychain id or something else?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: