embedding youtube video in iphone app

34,585

Solution 1

No need to use the embed code. The standard UIWebView aimed at the youtube web address should work fine...

// URL from safari address bar. 
NSURL *url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=fDXWW5vX-64"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

Solution 2

@danh answer does work, but it embeds youtube's mobile webpage with the youtube player, which takes a lot of space and it looks terrible. The best option is to follow the instructions in the Youtube Api Blog:

UPDATE: Since youtube now uses v3 of its api, here is a new link with the oficial instructions: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Solution 3

In iOS 8 it started working differently and added padding to the WebView. I posted an answer to this question, this works with iOS 9 and Xcode 7 beautifully. The padding (-8px) may need to be adjusted based on your needs but in general it works amazingly.

All you need to do it give it the code from any YouTube video (the last characters in any YouTube video URL)

See post here.

And here is the code:

    CGFloat width = self.webView.frame.size.width;
    CGFloat height = self.webView.frame.size.height;
    NSString *youTubeVideoCode = @"dQw4w9WgXcQ";
    NSString *embedHTML = @"<iframe width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" style=\"margin:-8px;padding:0;\" allowfullscreen></iframe>";
    NSString *html = [NSString stringWithFormat:embedHTML, width, height, youTubeVideoCode];
    self.webView.scrollView.bounces = NO;
    [self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];

Solution 4

Google provides a helper to embed and control youtube videos in iOS apps. Have a look at the documentation here: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Solution 5

I have used this in my app. this doesn't use any webview

https://www.cocoacontrols.com/controls/hcyoutubeparser

OR

NSURL *urlOfYouTube = [NSURL URLWithString:@"http://www.youtube.com..."];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlOfYouTube];
[self.webView loadRequest:requestObj];
Share:
34,585
Abilash
Author by

Abilash

Updated on July 09, 2022

Comments

  • Abilash
    Abilash almost 2 years

    I am new to IOS application development. I've added the code for embedding Youtube video in my app by using UIWebview and the embed code from the youtube. But when I run the application in my simulator, the webview is simply blank. I don't see any video thumbnail or anything else. I heard youtube videos will not run on IPhone simulators but this link ("http://www.youtube.com/embed/36db4r3MsgU")shows that the video is playing perfectly in simulator. kindly look into this link and suggest me a solution.

    NSString *code = @"<iframe width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/36db4r3MsgU?feature=player_detailpage\" frameborder=\"0\" allowfullscreen></iframe>";
    [[self video]loadHTMLString:code baseURL:nil];
    

    Thanks, Abilash.G