Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2012 18:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anonymous/2501684 to your computer and use it in GitHub Desktop.
Save anonymous/2501684 to your computer and use it in GitHub Desktop.
Simple iOS YouTube App - Use this code to put a list of videos from a YouTube user in your app.
//
// DetailViewController.h
// youtubedemo
//
// Created by Your Name on 4/24/12.
// Copyright (c) 2012 Company Name. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController {
IBOutlet UIWebView *webView;
IBOutlet UILabel *detailDescriptionLabel;
NSString *videoString;
NSString *titleString;
}
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSString *videoString;
@property (nonatomic, retain) NSString *titleString;
//- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;
@end
//
// DetailViewController.m
// youtubedemo
//
// Created by Your Name on 4/24/12.
// Copyright (c) 2012 Company Name. All rights reserved.
//
#import "DetailViewController.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize webView;
@synthesize detailDescriptionLabel;
@synthesize videoString, titleString;
#pragma mark - Managing the detail item
- (void) displayGoogleVideo:(NSString *)urlString frame:(CGRect)frame
{
NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><param name=\"movie\" value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%@\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>",urlString,urlString,frame.size.width,frame.size.height];
[webView loadHTMLString:htmlString baseURL:nil];
NSLog(@"HTML IS: %@", htmlString);
}
/*
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: transparent;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
NSLog(@"HTML IS: %@", html);
[webView loadHTMLString:html baseURL:nil];
}
*/
- (void)configureView
{
// Update the user interface for the detail item.
//NSLog(@"URL is:%@", videoString);
//[self embedYouTube:videoString frame:CGRectMake(70, 100, 200, 200)];
[self displayGoogleVideo:videoString frame:CGRectMake(0, 0, 320, 175)];
detailDescriptionLabel.text = titleString;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"YouTube";
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.detailDescriptionLabel = nil;
}
@end
//
// MasterViewController.h
// youtubedemo
//
// Created by Your Name on 4/24/12.
// Copyright (c) 2012 Your Company. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "GDataYouTube.h"
#import "GDataServiceGoogleYouTube.h"
@interface MasterViewController : UITableViewController {
GDataFeedYouTubeVideo *feed;
}
@property (nonatomic, retain) GDataFeedYouTubeVideo *feed;
@end
//
// MasterViewController.m
// youtubedemo
//
// Created by Your Name on 4/24/12.
// Copyright (c) 2012 Your Company. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController (PrivateMethods)
- (GDataServiceGoogleYouTube *)youTubeService;
@end
@implementation MasterViewController
@synthesize feed;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
NSLog(@"Loading");
GDataServiceGoogleYouTube *service = [self youTubeService];
NSString *uploadsID = kGDataYouTubeUserFeedIDUploads;
NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"JMEveryday"
userFeedID:uploadsID];
[service fetchFeedWithURL:feedURL
delegate:self
didFinishSelector:@selector(request:finishedWithFeed:error:)];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)request:(GDataServiceTicket *)ticket
finishedWithFeed:(GDataFeedBase *)aFeed
error:(NSError *)error {
self.feed = (GDataFeedYouTubeVideo *)aFeed;
[self.tableView reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[feed entries] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0f;
}
/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Configure the cell
GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry title] stringValue];
NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails];
cell.textLabel.text = title;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
cell.imageView.image = [UIImage imageWithData:data];
return cell;
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry title] stringValue];
NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails];
cell.textLabel.text = title;
// Load the image with an GCD block executed in another thread
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage * image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell setNeedsLayout];
});
});
dispatch_release(downloadQueue);
return cell;
}
- (GDataServiceGoogleYouTube *)youTubeService {
static GDataServiceGoogleYouTube* _service = nil;
if (!_service) {
_service = [[GDataServiceGoogleYouTube alloc] init];
[_service setUserAgent:@"AppWhirl-UserApp-1.0"];
//[_service setShouldCacheDatedData:YES];
[_service setServiceShouldFollowNextLinks:NO];
}
// fetch unauthenticated
[_service setUserCredentialsWithUsername:nil
password:nil];
return _service;
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailController"];
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
NSString *tempURL = [flashContent URLString];
//NSLog(@"The URL is:%@",tempURL);
//NSString *test = @"This is a test";
detailController.videoString = tempURL;
detailController.titleString = title;
// [self embedYouTube:[flashContent URLString] frame:CGRectMake(70, 100, 200, 200)];
[self.navigationController pushViewController:detailController animated:YES];
}
/*- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 200\"/></head> <body style=\"background:#F00;margin-top:0px;margin-left:0px\"> <div><object width=\"200\" height=\"200\"> <param name=\"movie\" value=\"%@\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"%@\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"200\" height=\"200\"></embed> </object></div></body></html>";
NSString *html =[NSString stringWithFormat:embedHTML, urlString, urlString, frame.size.width, frame.size.height];
[webView loadHTMLString:html baseURL:nil];
} */
@end
@rajara
Copy link

rajara commented Apr 4, 2013

Hello All,
Can you provide me some code to fetch gmail contacts into iPhone application.

@teknofreek
Copy link

can u tell me how to add the youtube api files/library to my xcode project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment