Your feedback is appreciated..Send Feedback Thank You.

How to embed Youtube video in UIWebView for iPhone?

Answer

In order to display a Youtube video, construct the htmlString as show in the snippet below. You can adjust the height and width as required.Normally 320×480 works best for iPhone. The Youtube video url is passed as input to the htmlString. Finally the UIWebView displays the video by rendering html loaded through the loadHTMLString method once its added to the view.

Snippet

- (void) displayGoogleVideo
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize screenSize = rect.size;

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
    webView.autoresizesSubviews = YES;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

    NSString *videoUrl = @”http://www.youtube.com/v/oHg5SJYRHA0″;

    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><object width="320" height="480"><param name="movie" value="%@"></param><param name="wmode" value="transparent"></param><embed src="%@" type="application/x-shockwave-flash" wmode="transparent" width="320" height="480"></embed></object></div></body></html>",videoUrl,videoUrl]    ;
    [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];

    [window addSubview:webView];
    [webView release]; // CLANG BUG FIXED

}

Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.

Tags: