Este não é o código mais bonito do mundo, é o resultado de apenas colocar as coisas em funcionamento. Não se esqueça de criar um projeto nas páginas relevantes do Google para obter acesso à API.
Eu também tive que usar a chave de API do navegador em vez da chave de API do iOS.
- (GTLServiceYouTube *)youTubeService {
static GTLServiceYouTube *service;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
service = [[GTLServiceYouTube alloc] init];
// Have the service object set tickets to fetch consecutive pages
// of the feed so we do not need to manually fetch them.
service.shouldFetchNextPages = YES;
// Have the service object set tickets to retry temporary error conditions
// automatically.
service.retryEnabled = YES;
});
return service;
}
- (id)init;
{
self = [super init];
if (self) {
self.videos = [[NSMutableArray alloc] init];
self.youTubeService.APIKey = @"your_api_key";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
GTLServiceYouTube *service = self.youTubeService;
GTLQueryYouTube *query;
query = [GTLQueryYouTube queryForGuideCategoriesListWithPart:@"snippet"];
query.regionCode = @"US";
query.hl = @"en-US";
__block NSMutableArray *blockVideos = self.videos;
// let's get the categories
ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *blockTicket, GTLYouTubeGuideCategoryListResponse *list, NSError *error) {
GTLYouTubeGuideCategory *cat = [list.items objectAtIndex:0];
GTLQueryYouTube *channelsQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id,snippet"];
channelsQuery.categoryId = cat.identifier;
channelsQuery.maxResults = 10; // only need one, but maxresults = 1 is slower than 10
// let's get the channels for the given category
__unused GTLServiceTicket *channelsTicket = [service executeQuery:channelsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannelListResponse *channelList, NSError *videoError) {
// we are only interested in one channel: the best of the best
[channelList.items enumerateObjectsUsingBlock:^(GTLYouTubeChannel *channel, NSUInteger idx, BOOL *stop) {
if( [channel.snippet.title isEqualToString:@"Popular on YouTube - Worldwide"] )
{
// get related playlists for our channel
GTLQueryYouTube *playlistsQuery = [GTLQueryYouTube queryForPlaylistsListWithPart:@"id,snippet"];
playlistsQuery.channelId = channel.identifier;
playlistsQuery.maxResults = 20;
__unused GTLServiceTicket *playlistsTicket = [service executeQuery:playlistsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubePlaylistListResponse *playlistsResponse, NSError *error) {
// list of related playlists: only 1
[playlistsResponse.items enumerateObjectsUsingBlock:^(GTLYouTubePlaylistItem *playlist, NSUInteger idx, BOOL *stop) {
NSLog(@"obj: %@", playlist.identifier);
GTLQueryYouTube *videosQuery = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
videosQuery.playlistId = playlist.identifier;
videosQuery.maxResults = 20;
__unused GTLServiceTicket *videosTicket = [service executeQuery:videosQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubePlaylistItemListResponse *playlistItemsResponse, NSError *error) {
[playlistItemsResponse.items enumerateObjectsUsingBlock:^(GTLYouTubePlaylistItem *playlistItem, NSUInteger idx, BOOL *stop) {
GTLYouTubeVideo *video = (GTLYouTubeVideo *)playlistItem.snippet.resourceId;
NSString *videoTitle = playlistItem.snippet.title;
NSString *videoId = [video.JSON valueForKey:@"videoId"];
[blockVideos addObject:@{@"title":videoTitle,@"identifier":videoId}];
}];
[self.tableView reloadData];
}];
}];
}];
return;
}
}];
}];
}];
}