/iOS-YouTube-Browser

iOS YouTube Browser Sample

Primary LanguageObjective-CMIT LicenseMIT

iOS YouTube Browser Sample

This tutorial explains how to create a simple iOS application which working with YouTube API.

First of all, you should create Google account, if you haven’t. Go to Google Developers Console and create the project.

alt tag

In created project, you will have lots of settings, statistics, something else. For your application we need to enable YouTube API.

alt tag

Also you need to create an iOS key by this link on the API access tab and Create new iOS key button.

alt tag

alt tag

Now all settings were set up.

We have two question for our simple application. How to receive data from YouTube? And how to play YouTube videos in UIKit?

For receiving the data we will use the next request. Google provides lots of information about API, you can found here. Full information about API, samples, etc.

The sample code for a request using AFNetworking:

static NSString * const YouTubeBaseUrl = @"https://www.googleapis.com/youtube/v3/search?part=snippet&q=%@&type=video&videoCaption=closedCaption&key=%@&maxResults=%@";
static NSString * const YouTubeAppKey = @"AIzaSyCs0lcHGW2oW88FO8FeR8j_hXMc9oCG6p0";
static const NSInteger YouTubeMaxResults = 50;

...

NSString *str = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:YouTubeBaseUrl, str, YouTubeAppKey, @(YouTubeMaxResults)];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
   NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   NSLog(@"Error: %@", error);
}];

And the second question. For this you can use the YouTube Player. It’s a great control with a really simple usage:

[self.playerView loadWithVideoId:@"M7lc1UVf-VE"];

I think it doesn’t make sense to explain the details, just download the repository and use the sample.

alt tag

That’s all. Happy coding!!!