title: How to get YouTube autoplay to work in UIWebView?
body: <p>Platform: iOS 6.1. Xcode 4.6.</p>
  
  <p>I am using YouTube Javascript API and a UIWebView. I am able to manually tap on the video thumbnail to begin playback just fine. But, autoplay is not working. I am setting mediaPlaybackRequiresUserAction = NO for the web view as many have suggested. No luck.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>@interface VideoPlayerController : UIViewController &lt;UIWebViewDelegate&gt;
      @property(strong, nonatomic) IBOutlet UIWebView* webView;
  @end
  
  - (void) viewDidLoad
  {
      [super viewDidLoad];    
  
      self.webView.delegate = self;
  
      NSString *template = [NSString stringWithContentsOfFile:
                         [[NSBundle mainBundle]
                          pathForResource:@"YouTubeTemplate" ofType:@"txt"]];
      NSString *htmlStr = [NSString stringWithFormat: template,
          200, 200,
          @"3G4exdlsRkY"];
      self.webView.mediaPlaybackRequiresUserAction = NO;
      [self.webView loadHTMLString:htmlStr baseURL:nil];
  }
  
  - (void) webViewDidFinishLoad:(UIWebView *)wv {
      self.webView.mediaPlaybackRequiresUserAction = NO;
  }
  </code></pre>
  
  <p>The YouTubeTemplate.txt file is:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="https://www.youtube.com/player_api"&gt;&lt;/script&gt;
  &lt;style&gt;
  body, div {
      margin: 0px;
      padding: 0px;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="media_area"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;script&gt;
  var ytPlayer = null;
  
  function onYouTubePlayerAPIReady() {
      ytPlayer = new YT.Player('media_area', {height: '%d', width: '%d', videoId: '%@',
          events: {'onReady': onPlayerReady}
      });
  }
  
  function onPlayerReady(e) {
      e.target.playVideo();
  }
  &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, basically, the UIWebView shows the video thumbnail. Then I have to tap on it to begin playback. Autoplay is not working. Any idea what I might be doing wrong?</p>