0

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?

Thanks Akansha

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.

Can suggest anything now?

<!DOCTYPE HTML>
<html>
<body>
<div id="player"></div>
<script>
    //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 && !done) { // NEED TO ADD FILE OPERATION HERE
            setTimeout(stopVideo, 6000);
            done = true;
        }
    }
    function stopVideo() {
        player.stopVideo();
    }
</script>
</body>
</html>
Akansha
  • 47
  • 1
  • 11

3 Answers3

1

yes, you can create a UIWebView with the HTML/JS Context and in the

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

you can catch the request and call the obj-c method. Or you create the same solution like Appcelerator: https://stackoverflow.com/a/2471774/644629

Community
  • 1
  • 1
CarlJ
  • 9,461
  • 3
  • 33
  • 47
  • i think the answer is not perfect user wants to call function from javascript not from some delegate method – The iOSDev Apr 17 '12 at 08:27
  • @AalokParikh maybe you should read the edit version of my post! – CarlJ Apr 17 '12 at 08:29
  • I want to call a function not a method. thing is, i am opening a javascript and it has a function, so whenever I enter that function I want to perform some action, which I am not able to do with Javascript. So I was wondering If I can call objective-c function inside that javascript function, then I can make my things work. – Akansha Apr 17 '12 at 09:06
  • @Akansha yes with the first method you can! – CarlJ Apr 17 '12 at 09:09
  • you need to set the window.location and catch the request in the shouldStartLoadWithRequest method – CarlJ Apr 17 '12 at 11:14
  • but in that case will the command comes back to the javascript again? i mean if I catch the request in shouldStartLoadWithRequest, then will that return back to the javascript at same point where it left from? – Akansha Apr 18 '12 at 06:04
  • you can call a JS function from your UIWebView with the stringByEvaluatingJavaScriptFromString method – CarlJ Apr 18 '12 at 09:09
1

You can't do it directly like in JAVA/ANDROID. Refer my answer below

iOS UIWebView Javascript - insert data -receive callbacks?

Community
  • 1
  • 1
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
0

You can create an objective c module and import it into your app and call it from JavaScript. Is this what your looking for:

http://developer.appcelerator.com/doc/mobile/iphone/module_sdk

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56