How to embed Youtube video in UIWebView for iPhone?

June 01, 2011


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.


– (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 = @”″;

    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:@””]];

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


How to invoke Mail app in iOS for sending email?What are the new features in iOS 5?
  • Mahesh M

    i have a webview in the iphone i between i have youtube video where it is clicked it is opening in to the full screen but i dont want that one it must have to play with in that image size is this possible for iphone

  • Developer Feed

    Try to set the inline property for the webview

    and at the same time, add another attribute
    playsinline=1 to the src link for the youtube embded code.

Like us on Facebook