An open API service indexing awesome lists of open source software.

https://github.com/maximbilan/ios-youtube-browser

iOS YouTube Browser Sample
https://github.com/maximbilan/ios-youtube-browser

ios ios-app objective-c tutorial youtube youtube-api

Last synced: 10 months ago
JSON representation

iOS YouTube Browser Sample

Awesome Lists containing this project

README

          

# 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](https://raw.github.com/maximbilan/ios_youtube_browser/master/img/img1.png)

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

![alt tag](https://raw.github.com/maximbilan/ios_youtube_browser/master/img/img2.png)

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

![alt tag](https://raw.github.com/maximbilan/ios_youtube_browser/master/img/img3.png)

![alt tag](https://raw.github.com/maximbilan/ios_youtube_browser/master/img/img4.png)

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](https://raw.github.com/maximbilan/ios_youtube_browser/master/img/img5.png)

That’s all. Happy coding!!!