title: Youtube API in IOS 7 and IOS 8
body: <p>I have this requirements for play youtube's videos in my app:</p>
  
  <ul>
  <li>Autoplay when container view Controller will appear</li>
  <li>Detect when video finish (i enable controls in this event)</li>
  <li>It's begins not full screen</li>
  <li>Support fullscreen portrail and landscape</li>
  <li>Full compatible with IOS 7 and 8</li>
  </ul>
  
  <p>First, i try youtube-ios-player-helper with this results:</p>
  
  <ul>
  <li>IOS 8: work it perfect</li>
  <li>IOS 7: The video pause every time on fullscreen and not played fluid.</li>
  </ul>
  
  <p>I thing this could be the better solution but i cant make it work fine in IOS 7.</p>
  
  <p>Second and actual option, i use the hybrid solution: 
  IOS 8 with youtube-ios-player-helper and IOS 7 with UIWebView and embed youtube player. Result:</p>
  
  <ul>
  <li>IOS 8: work it perfect</li>
  <li>IOS 7: work it but 1/5 videos autoplay not work, therefore video finish is not detected (i don't know why)</li>
  </ul>
  
  <p>I appreciate any suggestion about better solution,</p>
  
  <p>My actual code (second option):</p>
  
  <pre><code>-(void)youtubePlayerConfiguration{
  
  NSString *version = [[UIDevice currentDevice] systemVersion];
  
  
  if ([version floatValue] &gt;= 8.0) {
  
      NSDictionary *playerVars = @{
  
                                   @"playsinline" : @1,
                                   @"autoplay" : @1,
                                   @"showinfo" : @0,
                                   @"autohide" : @1,
                                   @"rel" : @0,
                                   @"modestbranding" : @1
                                   };
  
       youtubePlayer.delegate = self;
       [youtubePlayer loadWithVideoId:self.videoId playerVars:playerVars];
  
  } else {
      UIWebView *webView = [[UIWebView alloc] initWithFrame:youtubePlayer.frame];
      [webView setAllowsInlineMediaPlayback:YES];
      [webView setMediaPlaybackRequiresUserAction:NO];
      webView.delegate = self;
  
      [self.view addSubview:webView];
  
      NSString* embedHTML = [NSString stringWithFormat:@"\
                             &lt;html&gt;\
                             &lt;body style='margin:0px;padding:0px;'&gt;\
                             &lt;script type='text/javascript' src='http://www.youtube.com/iframe_api'&gt;&lt;/script&gt;\
                             &lt;script type='text/javascript'&gt;\
                             function onYouTubeIframeAPIReady()\
                             {\
                             ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady,onStateChange: onPlayerStateChange}})\
                             }\
                             function onPlayerReady(a)\
                             { \
                             a.target.playVideo(); \
                             }\
                             var done = false;\
                             function onPlayerStateChange(event) {\
                                 if (event.data == YT.PlayerState.PLAYING &amp;&amp; !done) {\
                                     setTimeout(stopVideo, 6000);\
                                     done = true;\
                                 }\
                                 if (event.data == YT.PlayerState.ENDED) {\
                                     window.location = 'callback:anything';\
                                 }\
                             }\
                             &lt;/script&gt;\
                             &lt;iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&amp;rel=0&amp;playsinline=1&amp;autoplay=1&amp;showinfo=0' frameborder='0'&gt;\
                             &lt;/body&gt;\
                             &lt;/html&gt;", 288,150, self.videoId];
      [webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
  
      webView.allowsInlineMediaPlayback = YES;
      webView.mediaPlaybackRequiresUserAction = NO;
      webView.scrollView.bounces = NO;
  
      youtubePlayer.hidden = YES;
  }
  
  }
  
  
  #pragma mark - Player YouTube Delegates
  
  -(void)playerViewDidBecomeReady:(YTPlayerView *)playerView{
      //[[NSNotificationCenter defaultCenter] postNotificationName:@"Playback started" object:self];
  
      [playerView playVideo];
  }
  
  - (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{
      if (state == kYTPlayerStateEnded) {
          ratingView.editable = YES;
          ratingView.alpha = 1;
          ratingView.userInteractionEnabled = YES;
      }
  }
  
  - (void)playerView:(YTPlayerView *)playerView receivedError:(YTPlayerError)error
  {
      NSLog(@"YTPlayerView : receivedError :%i",error);
  }
  
  #pragma mark - WebView Delegate
  
  - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
      NSLog(@"ENTRO - request:%@",request);
  
      if ( [[[request URL] scheme] isEqualToString:@"callback"] ) {
  
          NSLog(@"get callback");
          ratingView.editable = YES;
          ratingView.alpha = 1;
          ratingView.userInteractionEnabled = YES;
          return NO;
  
      }
  
      return YES;
  }
  </code></pre>