Using webview to play youtube video in iphone application

I am trying to watch a video in my iphone application using the following code:

-(void) viewDidLoad{
[super viewDidLoad];

UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];

    NSString *youTubeVideoHTML = @"<html><head>\
    <body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    // Populate HTML with the URL and requested frame size
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML,@"http://www.youtube.com/watch?v=ZiIcqZoQQwg" , 100, 100];

    // Load the html into the webview
    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];
}

Can someone tell me what I am doing wrong? I have seen this in a few examples, but still I can’t solve it!

+5
source share
3 answers

It looks like your HTML string is malformed.

Try the following:

- (void)viewDidLoad {

    [super viewDidLoad];

    float width = 200.0f;
    float height = 200.0f;

    NSString *youTubeURL = @"http://www.youtube.com/watch?v=ZiIcqZoQQwg";

    UIWebView *webView = [UIWebView new];
    webView.frame = CGRectMake(60, 60, width, height);

    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html><head>"];
    [html appendString:@"<style type=\"text/css\">"];
    [html appendString:@"body {"];
    [html appendString:@"background-color: transparent;"];
    [html appendString:@"color: white;"];
    [html appendString:@"}"];
    [html appendString:@"</style>"];
    [html appendString:@"</head><body style=\"margin:0\">"];
    [html appendFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"", youTubeURL];
    [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", width, height];
    [html appendString:@"</body></html>"];

    [webView loadHTMLString:html baseURL:nil];

    [self.view addSubview:webView];

}

It is worth noting that this code will only work on the device, it will not work in the simulator.

UPDATE ...

Due to changes made by YouTube, the above method is now incorrect. Use the method below instead.

- (void)viewDidLoad {

    [super viewDidLoad];

    float width = 200.0f;
    float height = 200.0f;

    NSString *youTubeToken = @"K95Q0VFyhA8";

    UIWebView *wv = [UIWebView new];
    webView.frame = CGRectMake(60, 60, width, height);

    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendString:@"<style type=\"text/css\">"];
    [html appendString:@"body {"];
    [html appendString:@"background-color: transparent;"];
    [html appendString:@"color: white;"];
    [html appendString:@"margin: 0;"];
    [html appendString:@"}"];
    [html appendString:@"</style>"];
    [html appendString:@"</head>"];
    [html appendString:@"<body>"];
    [html appendFormat:@"<iframe id=\"ytplayer\" type=\"text/html\" width=\"%0.0f\" height=\"%0.0f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"/>", width, height, videoToken];
    [html appendString:@"</body>"];
    [html appendString:@"</html>"];

    [wv loadHTMLString:html baseURL:nil];

    [self.view addSubview:wv];

}
+8
source

try this simple one:

//get the youtube video token from youtube url //https://www.youtube.com/watch?v=youTubeToken

 NSString *youTubeToken = @"K95Q0VFyhA8";

//create webview..
    UIWebView *webView = [UIWebView new];
    webView.frame = CGRectMake(10, 10, 300, 300);

//load webview with the load request..
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/embed/%@",youTubeToken]]]];
    [self.view addSubview:webView];
0
source

All Articles