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>
0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

i think the answer is not perfect user wants to call function from javascript not from some delegate method
@AalokParikh maybe you should read the edit version of my post!
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 yes with the first method you can!
you need to set the window.location and catch the request in the shouldStartLoadWithRequest method
|
1

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

iOS UIWebView Javascript - insert data -receive callbacks?

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.