title: Capturing fullscreen event for fullscreen Youtube iFrame and do the fullscreen programmatically
body: <p>I embedded a YouTube iFrame with iFrame API into a <code>UIWebView</code> to play videos. Here's the HTML script:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;style&gt;
          body { margin: 0; width:100%; height:100%; }
          html { width:100%; height:100%; }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="player"&gt;&lt;/div&gt;
  
      &lt;script type='text/javascript' src="https://www.youtube.com/iframe_api" &gt;&lt;/script&gt;
      &lt;script type='text/javascript'&gt;
      var player;
      YT.ready(function() {
        player = new YT.Player('player', {
    "playerVars" : {
      "enablejsapi" : 1,
      "modestbranding" : 1,
      "autoplay" : 1,
      "showinfo" : 1,
      "controls" : 1,
      "playsinline" : 1,
      "autohide" : 0
    },
    "width" : "100%",
    "videoId" : %@,
    "events" : {
      "onPlaybackQualityChange" : "onPlaybackQualityChange",
      "onStateChange" : "onStateChange",
      "onError" : "onPlayerError",
      "onReady" : "onReady"
    },
    "height" : "100%"
  });
        window.location.href = 'ytIosPlayer://onYouTubeIframeAPIReady';
               });
      function onReady(event) {
          window.location.href = 'ytIosPlayer://onReady?data=' + event.data;
      }
      function onStateChange(event) {
          window.location.href = 'ytIosPlayer://onStateChange?data=' + event.data;
      }
  
      function onPlaybackQualityChange(event) {
          window.location.href = 'ytIosPlayer://onPlaybackQualityChange?data=' + event.data;
      }
      function onPlayerError(event) {
          window.location.href = 'ytIosPlayer://onError?data=' + event.data;
      }
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It is named "youtubeEmbedHtml.html" will be used in a class called <code>YoutubeIosPlayerView</code> that will have the UIWebView that will be used to embed the video. This is the code:</p>
  
  <pre><code>@implementation YoutubeIosPlayerView
  
  @synthesize webView;
  
  - (void)loadVideo:(NSString*)youtubeID
  {
      NSError *error = nil;
      NSString *path = [[NSBundle mainBundle] pathForResource:@"youtubeEmbedHtml"
                                                       ofType:@"html"
                                                  inDirectory:@"Assets"];
      NSString *embedHTMLTemplate =
          [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&amp;error];
      NSString *embedHTML = [NSString stringWithFormat:embedHTMLTemplate, youtubeID];
  
      //NSLog(@"YTPlayer embed HTML: %@", embedHTML);
      [self.webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"about:blank"]];
      [self.webView setDelegate:self];
      self.webView.allowsInlineMediaPlayback = YES;
      self.webView.mediaPlaybackRequiresUserAction = NO;
  
  }
  
  -(void)playVideo
  {
      NSLog(@"Video Played!");
      [self stringFromEvaluatingJavaScript:@"player.playVideo();"];
  }
  
  - (void)pauseVideo
  {
      NSLog(@"Video Paused!");
      [self stringFromEvaluatingJavaScript:@"player.pauseVideo();"];
  }
  
  - (void)stopVideo
  {
      NSLog(@"Video Stopped!");
      [self stringFromEvaluatingJavaScript:@"player.stopVideo();"];
  }
  
  - (void)clearVideo
  {
      [self stringFromEvaluatingJavaScript:@"player.clearVideo();"];
  }
  
  
  - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  {
  
      NSString* currentURL = self.webView.request.URL.absoluteString;
      NSLog(@"shouldStartLoadWithRequest: URL string = %@\n", currentURL);
  
      if ([request.URL.scheme isEqual:@"ytIosPlayer"])
      {
          NSString *action = request.URL.host;
  
          NSString *query = request.URL.query;
          NSString *data;
          if (query)
          {
              data = [query componentsSeparatedByString:@"="][1];
          }
  
          if ([action isEqual:@"onReady"])
          {
              if ([self.delegate respondsToSelector:@selector(playerViewDidBecomeReady:)])
              {
                  [self playVideo]; //Play video if it's successfully loaded
              }
          }
          else if ([action isEqual:@"onStateChange"])
          {
              if ([self.delegate respondsToSelector:@selector(playerView:didChangeToState:)])
              {
  
                  if ([data isEqual:@"0"])
                  {
                      [self loadNextVideo]; //Load the next video automatically when the current video stops playing
                  }
              }
          }
  
          return NO;
      }
      else if ([request.URL.scheme isEqual: @"http"] || [request.URL.scheme isEqual:@"https"])
      {
          return [self handleHttpNavigationToUrl:request.URL];
      }
      return YES;
  }
  
  -(void)webViewDidFinishLoad:(UIWebView *)webView
  {
      NSString* currentURL = self.webView.request.URL.absoluteString;
      NSLog(@"webViewDidFinishLoad: URL string = %@\n", currentURL);
      NSString* location = [self.webView stringByEvaluatingJavaScriptFromString:@"window.location"];
      NSLog(@"webViewDidFinishLoad: Location string = %@\n", location);
      NSString* html = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
      NSLog(@"webViewDidFinishLoad: inner HTML string = %@\n", html);
      NSLog(@"--------------------------------------------------------------------------------");
  }
  </code></pre>
  
  <p>Now, the code is actually working quite well. But due to some circumstances, I need to be able to capture the event when the user is clicking the fullscreen button and whether or not the player is playing at fullscreen. Also I need to be able to do the fullscreen programmatically from the app instead of clicking the fullscreen button in iFrame. Can anybody tell me how to do so?</p>
  
  <p>Thanks in advance.</p>