title: Knowing when a YouTube video starts playing from within a UIWebView
body: <p>I have made a class that plays YouTube videos (thanks to the help from the kind people of this site).</p>
  
  <p>Now I am wondering how I find out when a YouTube video starts playing. I want to know this so I can show a "Please Wait, Loading..." indicator in-between the video being told to play and it actually playing.</p>
  
  <p>I start the video playing using <code>stringByEvaluatingJavaScriptFromString:</code>, and of course there is a slight delay between me firing off the javascript command to get the video playing and the video actually playing.</p>
  
  <p>I am familiar with YouTube videos and javascript, so Ive been looking at this: [ <a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html</a> ] because I am wondering if a javascript event listener inside the UIWebView could detect when a YouTube video starts playing (listening for the <code>onStateChange</code> event) and fire off an Objective C method.</p>
  
  <p>Is this possible? Do UIWebViews run javascript in the same way as a web browser would?</p>
  
  <p>Is there an easier / better way of doing this?</p>
  
  <p>Here is my youtube class:</p>
  
  <pre><code>// this is the only property declared in the .h file:
  @property (strong, nonatomic) UIView * view
  
  // the rest of this is the .m file:
  #import "MyYouTube.h"
  @interface MyYouTube()
  @property (strong, nonatomic) NSDictionary * contentData;
  @property (strong, nonatomic) UIWebView * webView;
  @property (nonatomic) int videoOffset;
  @end
  
  @implementation MyYouTube
  @synthesize view,contentData,webView,videoOffset;
  
  - (MyYouTube*) initIntoView: (UIView*) passedView withContent: (NSDictionary*) contentDict {
      NSLog(@"YOUTUBE: begin init");
      self=[super init];
      videoOffset=0;
      view=[[UIView alloc] initWithFrame:passedView.bounds];
      [view setBackgroundColor:[UIColor blackColor]];
      [view setAutoresizesSubviews:YES];
      [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
      contentData=contentDict;
  
      NSString * compiledUrl=[[NSString alloc] initWithFormat:@"http://_xxx_.com/app/youtube.php?yt=%@",[contentData objectForKey:@"cnloc"]];
      NSURL * url=[[NSURL alloc] initWithString:compiledUrl];
      NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];
  
      webView=[[UIWebView alloc] initWithFrame:passedView.bounds];
      [webView loadRequest:request];
      [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
      [[webView scrollView] setScrollEnabled:NO];
      [[webView scrollView] setBounces:NO];
      [webView setMediaPlaybackRequiresUserAction:NO];
      [webView setDelegate:self];
  
      NSLog(@"YOUTUBE: self: %@",self);
      NSLog(@"YOUTUBE: delegate: %@",webView.delegate);
  
      [view addSubview:webView];
      NSLog(@"YOUTUBE: end init");
      return self;
  }
  
  -(void)webViewDidFinishLoad:(UIWebView*)myWebView {
      NSLog(@"YOUTUBE: send play command");
      [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"playVideo(%d)", videoOffset]];
  }
  
  @end
  </code></pre>
  
  <p>Many thanks.</p>