Breaking News
Home / iOS / iOS Snippets / How to embed Youtube video in UIWebView for iPhone?

How to embed Youtube video in UIWebView for iPhone?


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


Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …


  1. 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

  2. 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.

Leave a Reply