YouTube API for iOS – Uploading a Video

Page 1

YouTube API for iOS – Uploading a Video As straightforward as it may seem, one can get lost trying to get their app to use the YouTube API. This tutorial will explain how to use the API to get a list of videos in a playlist and then upload a video right from our app! These are the basic steps we will be following: 1. Create a iOS project in Xcode. 2. Register an App with Google. 3. Get the YouTube Data API. 4. Integrate the API into your project. 5. Configure the API for your Google App. 6. Use the API to get Playlist data and upload a video. Lets get started. 1) Create a iOS project in Xcode. Obviously we will need a iOS Application project. So go ahead and create a new iOS project in Xcode. Note the bundle ID of the project from the plist file, we will need this later.


2) Register an App with Google. We need to register an application with Google. This is needed so the API requests coming to YouTube from your App can be identified. It is pretty straightforward. Firstly, you will need a google account. Then head over to Google Developers Console. Here you will see a projects tab with a ‘ CREATE PROJECT’ button, click it. This will present you with a window where you need to provide some project details. First is Project Name, self explanatory. Next is Project ID which is auto-generated but can be changed. However it needs to be unique globally! I suggest you use the one provided by default as its not visible to the end user anyway. Read and accept the Terms of Service to continue. If the information you provided was valid, you should be redirected to your Project’s page. Note that this may take some time while your project request is being approved. Click on the ‘APIs and Auth’ tab and then select ‘Credentials’. You should see a OAuth section with Client ID and Email Address. However these are not the credentials we are interested in. We need credentials for a native app. Click the ‘CREATE NEW CLIENT ID’ button. In the Create Client ID window, select ‘Installed application’, followed by ‘iOS’ for type. Next you will need to provide your Application’s Bundle ID,


this is found in the .plist file of your project or in the Project settings window under General. Hit Create button to register. You will get a new Client ID and Client secret, this is what we will need later. 3) Get the YouTube Data API At the time of writing this tutorial the latest version of Google Data API was ‘v3′. The first thing we will need to do is download the objective-C client library. *You can get the latest url here. This will create a ‘google-api-objectivec-client-read-only‘ directory at the location you chose. 4) Integrate the API into your project There are 3 ways to integrate the client library into your project: as a iOS static library, as a framework or directly by including source files. I have found the last option to be most convenient and thats what I will be using in this tutorial. In case you want to use a different approach, this link will be helpful. We will need to include a bunch of files into the project. Create a new group within the project, (mine is called ‘Google-API-Stuff’) to put all these files in.


4.1) Add the ‘GTLCommon_Sources.m’ and ‘GTLNetworking_Sources.m’ files to your project. Open the downloaded client API folder we just checked out. Open the ‘Source’ folder. Here you should see these two files. Copy them into your project, under the new group we just created. Make sure you DO NOT select the ‘Copy items into destination group’s folder’ option. 4.2) Adding library’s source folders to Header Search Paths This one requires some work. What will do here is tell Xcode where to find the relevant files for the Google API from our machine instead of copying it into our project. a) In Xcode navigator select your project file. b) Make sure you have the project selected and not the target. c) Click the ‘Build Settings’ tab. d) In the search box, type ‘Header Search Paths’ e) You should now see the entry for ‘Header Search Paths’ below. Double click it to open the edit window. f) You can click the little ‘+’ button at the bottom to add the paths required. g) We need to add the FULL paths for the following folders: Source


Source/Objects Source/Utilities Source/HTTPFetcher Source/OAuth2 Source/OAuth2/Touch For example in my case the paths are: /Users/vicky/Documents/Development/Personal/Resources/google-apiobjectivec-client-read-only/Source /Users/vicky/Documents/Development/Personal/Resources/google-apiobjectivec-client-read-only/Source/Objects /Users/vicky/Documents/Development/Personal/Resources/google-apiobjectivec-client-read-only/Source/Utilities h) Now that we have added the common files required for the API, we will need to repeat the above steps for the particular google service we wish to use, in our case YouTube :] Open the downloaded client API folder again and navigate to Source/Services/YouTube/Generated. From here copy the ‘GTLYouTube_Sources.m’ to your project group where we placed the previous copied files.


Next we will need to add this path to the Header Search Paths like we did earlier. This time the folder we need is: Source/Services/YouTube/Generated In my case the full path is: /Users/vicky/Documents/Development/Personal/Resources/google-apiobjectivec-client-read-only/Source/Services/YouTube/Generated 4.3) Add OAuth 2 sign-in view YouTube services require OAuth to establish a secure connection with the client. We need to add this view to our project as well. In the client API folder, navigate to: /Source/OAuth2/Touch Here you will see GTMOAuth2ViewTouch.xib. Copy it into your project as well. 4.4) Add required frameworks We need to following frameworks linked to our project. a) Security.framework b) SystemConfiguration.framework 4.5) ARC


In case you are using ARC, which you should, you will need to perform another step. The API files we are compiling do not use ARC while our project does. Therefore we will need to specifically tell Xcode to treat these files as ‘not using ARC’ . Head over to the Build Phases of your target. Here under compile sources, you should see the three API .m files we added to our project along with other files from our project. We need to add a flag to these three files to mark them as non ARC. Double click a file, this will open a window, insert the following flag: -fno-objc-arc Do this for all the three files (GTLCommon_Sources.m, GTLNetworking_Sources.m, GTLYouTube_Sources.m). This should do it! Our project is now ready to link with the API. Build project, there should be no errors. 5) Configure the API for your Google App. Basically our App is now ready to talk to YouTube. All we have to do is authenticate and then call appropriate API functions to get/send data. To authenticate we will need the Client ID and Client Secret that we got earlier. After this, the user will need to log into her/his google account and provide permission to our app to use YouTube on their behalf.


Though this process is not very complicated it get a little confusing at first. I wrote a simple wrapper class when I was working with this API. Please use it for now to make things easier. Later you can modify it or write your own class for any particular requirement. Download the YouTube Helper here. Add the YouTubeHelper.h and YouTubeHelper.m files to your project. To work with YouTubeHelper, include the ‘YouTubeHelper.h’ file into your viewController. Your class will also need to conform to the YouTubeHelperDelegate protocol. #import<UIKit/UIKit.h> #import“YouTubeHelper.h” @interface ViewController : UIViewController @end

The protocol has some mandatory methods: – (NSString

*)youtubeAPIClientID;

Here you will provide your app’s Client ID that we got from the Google Developer Console earlier. – (NSString *)youtubeAPIClientSecret;

Here you will provide the Client Secret. – (void)showAuthenticationViewController:(UIViewController *)authView;

When the OAuth process starts, the API needs to present its view controller to the user. YouTubeHelper calls this delegate method and provides you with the viewController to be displayed. It is then your responsibility to push/present this.


– (void)authenticationEndedWithError:(NSError *)error;

After authentication this delegate is called. If the error is nil, it means the authentication was successful. Ok done, now what? After implementing the delegates, its time to start using the API. Inside your class create an object of the YouTubeHelper class using the initWithDelegate: method. Now that you have an instance of this object, you can use it to authenticate, get uploaded videos playlist and upload a video. a) Authenticate: User will need to authenticate the application for the first use. After auth the information is stored in user’s keychain and can be reused on subsequent runs. To authenticate, call the – (void)authenticate;

method on YouTubeHelper object. This will be followed by the two delegate methods discussed earlier being called. In case the user wishes to sign out, call the – (void)signOut;


method. This will delete the auth info stored in keychain. b) Getting uploaded playlist: If the authentication is done, we can now finally get some useful information from youtube. Call the – (void)getUploadedPlaylist;

method. This will fetch the upload channel and a list of videos uploaded to it. After the data is fetched the – (void)uploadedVideosPlaylist:(NSArray *)array;

delegate is called. This will return an array of ‘GTLYouTubePlaylistItem’ items. To test this you can print the names of the items using item.snippet.title. – (void)uploadedVideosPlaylist:(NSArray *)array; { NSLog(@”uploaded list:”); for (int ii = 0; ii < array.count; ii++) { GTLYouTubePlaylistItem* item = array[ii]; NSLog(@” %@”, item.snippet.title); } }

Make sure you have a video uploaded on youtube before testing this. c) Uploading a video. To upload a video, you will need to call the – (void)uploadPrivateVideoWithTitle:(NSString *)title description:(NSString *)description


commaSeperatedTags:(NSString *)tags andPath:(NSString *)path; method.

It accepts a title (mandatory), description (optional), tags (optional) and path (mandatory). You can track the upload progress by implementing the – (void)uploadProgressPercentage:(int)percentage;

method. For additional help on using YouTubeHelper check out the demo project. However note that this project will not run on your machine until you set up all the links mentioned above.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.