title: How to call Objective-c function from JavaScript?
body: <p>I am trying to make an iphone app. In that app I am using Javascript. I want to know it is possbile to call a objective C function function from Javascript. If yes, then how can we do that? If not then is there any alternative for that?</p>
  
  <p>Thanks
  Akansha</p>
  
  <p>I have this java script code. So whenever I enter in "onPlayerStateChange" function I want to open a file and write some event in this file. But I am not able to open and write file in javascript as the location of file is not known to me. With objective-c I can open and write data in documentsDirectory, but I am not sure about javascript. So I was thinking to call a function inside "onPlayerStateChange" which is there in objective-C .m file. and that function writes in the file whenever I enter "onPlayerStateChange" function.</p>
  
  <p>Can suggest anything now?</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;div id="player"&gt;&lt;/div&gt;
  &lt;script&gt;
      //Load player api asynchronously.
      var tag = document.createElement('script');
      tag.src = "http://www.youtube.com/player_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      var done = false;
      var player;
      function onYouTubePlayerAPIReady() {
          player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'JW5meKfy3fY',
            events: {
              'onReady': onPlayerReady,
              'onStateChange': onPlayerStateChange
            }
          });
      }
      function onPlayerReady(evt) {
          evt.target.playVideo();
      }
      function onPlayerStateChange(evt) {
          if (evt.data == YT.PlayerState.PLAYING &amp;&amp; !done) { // NEED TO ADD FILE OPERATION HERE
              setTimeout(stopVideo, 6000);
              done = true;
          }
      }
      function stopVideo() {
          player.stopVideo();
      }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>