quinta-feira, 15 de dezembro de 2011

Função - Colocar um video do Youtube dentro de um UIWebView

ATENÇÃO: Não funciona no Simulador. Apenas no device.




  1. // ViewController.m
  2. - (void)viewDidLoad
  3. {
  4.     [super viewDidLoad];
  5.  // Do any additional setup after loading the view, typically from a nib.
  6.    
  7.     NSString * link    = @"http://www.youtube.com/watch?v=U8x-QjBHP8M";
  8.     CGRect     frame   = CGRectMake(20, 20, 280, 280);
  9.    
  10.     [self embedYouTube:link andFrame:frame andView:self.view];
  11. }
  12. //Método para colocar um video do youtube em um UIWebView
  13. -(void)embedYouTube:(NSString *)urlString andFrame:(CGRect)frame andView:(UIView *)view
  14. {
  15.     NSString *embedHTML = @"\
  16.    <html><head>\
  17.    <style type=\"text/css\">\
  18.    body {\
  19.    background-color: transparent;\
  20.    color: white;\
  21.    }\
  22.    </style>\
  23.    </head><body style=\"margin:0\">\
  24.    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
  25.    width=\"%0.0f\" height=\"%0.0f\"></embed>\
  26.    </body></html>";
  27.     NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
  28.     UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
  29.     [videoView loadHTMLString:html baseURL:nil];
  30.     [view addSubview:videoView];
  31.     [videoView release];
  32. }


Donwload: VideoYoutube.zip