5

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

Abilash
  • 155
  • 1
  • 1
  • 9

8 Answers8

10

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];
Duck
  • 34,902
  • 47
  • 248
  • 470
danh
  • 62,181
  • 10
  • 95
  • 136
7

@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

Nick
  • 1,032
  • 16
  • 27
  • 2
    Please, try to read this http://stackoverflow.com/help/deleted-answers, to get more understanding how to **not** answer. Namely: "Answers that do not fundamentally answer the question": **barely more than a link to an external site** – Radim Köhler Sep 26 '13 at 02:16
  • http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html goes to an empty page – Dave Haigh Jul 14 '15 at 08:17
  • @DaveHaigh thank you for telling me, youtube api is now on V3, many things have changed. I have updated my answer – Nick Jul 15 '15 at 20:26
4

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"]];
Oksana
  • 109
  • 5
  • 13
Ethan Parker
  • 2,986
  • 1
  • 23
  • 29
  • By the way, you have the width and height swapped in the first two lines (CGFloat width = self.webView.frame.size.height), etc. – Nik Bhatt Mar 25 '18 at 20:22
2

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];
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
2

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

Ali
  • 18,665
  • 21
  • 103
  • 138
1

// MY UIWebview is named "videoYoutube" in this example below // UIWebview size = 320x180 // This below will set the width to the guides ... or close to it and scale dependent on the device size.

    let bounds = UIScreen.mainScreen().bounds
    let width = bounds.size.width - 18
    let height = bounds.size.width / 1.8
    let frame = 0

    let youtubeVideolink = "https://www.youtube.com/embed/Rg6GLVUnnpM"

    let Code:NSString = "<iframe width=\(width) height=\(height) src=\(youtubeVideolink) frameborder=\(frame) allowfullscreen></iframe>"

    self.videoYoutube.loadHTMLString(Code as String, baseURL: nil)
Steve B
  • 861
  • 1
  • 7
  • 9
1

It is too much easy in swift 4

let url = URL(string: "http://www.youtube.com/watch?v=fDXWW5vX-64")

let request = URLRequest(url: url!)

webView?.load(request)

Quiet Islet
  • 536
  • 1
  • 8
  • 28
0

For the first suggested answer i faced some issues like the whole youtube page has been loaded for fixing that i used some extra codes, the codes are

NSString *baseUrl = [NSString stringWithFormat:@"%@%@",@"http://www.youtube.com/watch?v=",embedCode];
    NSURL *url = [NSURL URLWithString:baseUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    videoWebView = [[UIWebView alloc] initWithFrame:imageview.frame];
    [videoWebView loadRequest:request];
    [videoWebView setNeedsLayout];
    [videoWebView setAllowsInlineMediaPlayback:YES];
    videoWebView.mediaPlaybackRequiresUserAction = YES;
    videoWebView.scrollView.scrollEnabled = NO;
    videoWebView.scrollView.bounces = NO;
    [self.contentView addSubview:videoWebView];
R. Mohan
  • 2,182
  • 17
  • 30