10

I'm facing problems that I can't seem to understand.

I have this code that works perfectly in Xcode 5 with iOS 7:

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  CGFloat width = self.view.frame.size.width;
  CGFloat height = self.view.frame.size.height;
  UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
  NSString* embedHTML = @"\
  <html><head><meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; user-scalable=0;\"/>\
  <style type=\"text/css\">\
    body {\
    background-color: transparent;\
   color: black;\
  }\
  </style>\
  </head><body style=\"margin:0;\">\
  <embed id=\"yt\" src=\"https://www.youtube.com/v/M7lc1UVf-VE?hd=1\" type=\"application/x-shockwave-flash\" \
  width=\"%0.0f\" height=\"%0.0f\"></embed>\
  </body></html>";
  NSString *html = [NSString stringWithFormat:embedHTML, width, height];
  [webview loadHTMLString:html baseURL:nil];
  [self.view addSubview:webview];
}

When I build the same code in Xcode 6 for iOS 8 then the video displays in the webview but is way to small.

Can anybody explain what's happening here and how I could overcome this?

berliner
  • 499
  • 1
  • 6
  • 20

2 Answers2

5

I had the same problem, I solve using this solution

https://developers.google.com/youtube/v3/guides/ios_youtube_helper

also in the YTPlayerView.m (~line: 610) try this changes:

[playerParams setValue:[NSString stringWithFormat: @"%0.00f", self.frame.size.height] forKey:@"height"];
// [playerParams setValue:@"100%" forKey:@"height"];
[playerParams setValue:[NSString stringWithFormat: @"%0.00f", self.frame.size.width] forKey:@"width"];
// [playerParams setValue:@"100%" forKey:@"width"];

Hope this help

WalexM
  • 76
  • 1
  • Hm, thanks for the tip, but I'm actually more interested in what has changed that made my code fail now. I could build an iframe myself, but I'd prefer to understand why the embed technique is not working anymore, or more precisely why I can't control the size of the video anymore in iOS 8. Or how I could control it. – berliner Sep 24 '14 at 20:22
  • This player cannot play private videos, but it can play unlisted videos. Since this library wraps the existing iframe player, the player's behavior should be nearly identical to that of a player embedded on a webpage in a mobile browser. – Kirit Vaghela Jan 22 '15 at 06:52
3

I worked on this for a while and figured out a solution that works amazingly for my purposes. What was happening in my case is for some reason the iFrame was using some padding that needed to be discarded.

Also important to note is if you include youtube.com as the Base URL it will load much quicker.

Make a UIWebView in your storyboard and connect the @property to it, then reference below.

    CGFloat height = self.webView.frame.size.height;
    CGFloat width = self.webView.frame.size.width;
    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"]];
Kian
  • 122
  • 13
Ethan Parker
  • 2,986
  • 1
  • 23
  • 29