Embedding YouTube Videos in a Grid View in iPhone App.

In many projects, YouTube videos are embedded and there is a need to display these images in grid view. For displaying videos in grid view, we have to create another view for thumbnail view, create objects for this view and add it to the main view.

However, I have a simpler and effective solution for this:

Sample Project for YouTubes in GridView:

Detail:

I am using following classes:

              —SampleProjMoreVideosController.h and
SampleProjMoreVideosController.m

             —CustomCell.h and CustomCell.m

Two Nib files:

         — SampleProjMoreVideosController.xib:

         — CustomCell.xib:

Logic: I have used table view and custom table view cell.In custom table view cell ,I have placed three web views and embedding the YouTube videos.

CustomCell.h:

#import 
@interface CustomCell : UITableViewCell 
{
 IBOutletUIWebView* _webView;
IBOutletUIWebView* _webView2;
IBOutletUIWebView* _webView3;
}
@property (nonatomic, retain) IBOutlet UIWebView* _webView;
@property (nonatomic, retain) IBOutlet UIWebView* _webView2;
@property (nonatomic, retain) IBOutlet UIWebView* _webView3;
@end
CustomCell.m

#import "CustomCell.h"
@implementation CustomCell
@synthesize _webView,_webView2,_webView3;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    returnself;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state.
}
- (void)dealloc 
{
[_webViewrelease];
[_webView2release];
[_webView3release];
[superdealloc];
}
@end
SampleProjMoreVideosController.h

 #import 

#import "customCell.h"



@interface SampleProjMoreVideosController : UIViewController 

{

 UITableView*               _table;

 NSMutableArray*            _arrayOfVideosLinks;

NSMutableString*             _videoUrl; 

CustomCell*                  cell;

NSInteger                    numberOfVideos;

NSMutableString*            _htmlString;

}

@property (nonatomic,  retain)   IBOutlet   UITableView*       _table;

@property (nonatomic,  retain)   IBOutlet  NSMutableArray*    _arrayOfVideosLinks;

@property (nonatomic,  retain)   IBOutlet   NSMutableString*   _videoUrl;

@property (nonatomic,  retain)   IBOutlet   NSMutableString*    _htmlString;

//Method Declarations

-  (void)  getWebView: (NSNumber*) index;

-  (void)  setInitial;

 

@end
EyeDocMoreVideosController.m

#import "SampleProjMoreVideosController.h"
#import "CustomCell.h"
@implementation SampleProjMoreVideosController

@synthesize   _videoUrl,_table;

@synthesize _arrayOfVideosLinks,_htmlString;

 -(void) viewWillAppear:(BOOL)animated

{

 self.navigationController.navigationBar.hidden=FALSE;

}

 

- (void)viewDidLoad 

{

 [superviewDidLoad];

 [selfsetInitial];

   _table.rowHeight=100;

 self.title=@"Videos";

}

 - (void) setInitial

{

  _arrayOfVideosLinks=[[NSMutableArrayarrayWithObjects:

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=fcnQ72sPZAI"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=QJncFirhjPg"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=_SeNSMeq0Q4"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=52z-zfW3UyA"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=ae_DKNwK_ms"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=yuTgEfsGOXA"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=rQl7LRl7uB4"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=8RMWO9JxZjA"],

   [NSStringstringWithFormat:@"http://www.youtube.com/v/FAfiuGlubjU?fs=1&hl=en_US"],

   [NSStringstringWithFormat:@"http://www.youtube.com/watch?v=hJ57xzo287U"],

  nil] retain];

 numberOfVideos=[_arrayOfVideosLinkscount];

}

- (void) getWebView:(NSNumber*)index

{

 _videoUrl=[_arrayOfVideosLinksobjectAtIndex:[index integerValue]];

 _htmlString =[NSStringstringWithFormat:@""

          ""

          "document.ontouchmove = function(e){ alert('Hi'); e.preventDefault(); }"

          ""

  ""

  ""

  "
" "" "" "" "",_videoUrl,_videoUrl]; } - (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { if([_arrayOfVideosLinkscount]%3==0) return [_arrayOfVideosLinkscount]/3; else return [_arrayOfVideosLinkscount]/3+1; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { returnnil; } - (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { static NSString* CellIdentifier = @"CustomCell"; cell =(CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray* topLevelObjects=[[NSBundlemainBundle] loadNibNamed:@"customCell"owner:selfoptions:nil]; for(id currentObject in topLevelObjects) { if([currentObject isKindOfClass:[UITableViewCell class]]) { cell=(CustomCell *)currentObject; id scrollView = [cell._webView.subviews objectAtIndex:0]; [scrollView setScrollEnabled:NO]; scrollView = [cell._webView2.subviews objectAtIndex:0]; [scrollView setScrollEnabled:NO]; scrollView = [cell._webView3.subviews objectAtIndex:0]; [scrollView setScrollEnabled:NO]; break; } } } int key=indexPath.row*3; NSNumber* index=[NSNumber numberWithInt:key]; [self getWebView:index]; [cell._webViewloadHTMLString:_htmlStringbaseURL:[NSURLURLWithString:_videoUrl]]; key++; if(key>=numberOfVideos) { } else { index=[NSNumber numberWithInt:key]; [self getWebView:index]; [cell._webView2loadHTMLString:_htmlStringbaseURL:[NSURLURLWithString:_videoUrl]]; key++; if(key
150 150 Burnignorance | Where Minds Meet And Sparks Fly!