2

I found a lots of ways to call objective-c code from javascript, but I want to call the javascript code from objective-c. Last time I submitted a HTML FORM from objective-c, and now I wan't to call a javascript method. What do you think, is there any way to call it and get the response? I am interested in any solution, but I started to think and I think I need to send a html call or something like this, but I am not sure about this because the javascript is client side code, so maybe I need to process it from my objective-c code. What do you think about this?

2 Answers 2

2

Are you looking for UIWebView's stringByEvaluatingJavaScriptFromString: method?

This lets you run JS code inside of the sandbox of a web view, from Objective-C.

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

1 Comment

Look my comment at ExitToShell's answer. And thanks for you too. :)
1

Sounds like you are using a WebKit embedded in your app to link JavaScript to your "native" Obj-C code right?

If so you simply do this:

    NSString* script = @"function __wrapper() { return (typeof webNodeEvent == \"function\") } __wrapper();";

    id resultObj = [nodeScriptObject evaluateWebScript:script];

    if ([resultObj boolValue]) { // its there, call it

        [nodeScriptObject callWebScriptMethod:@"webNodeEvent" withArguments:[NSArray arrayWithObject:eventType]];

    }

The nodeScriptObject is from the WebView and is just the JavaScript object.

2 Comments

That's very cool. I didn't imagine that it is as simple as you too said. Thanks a lot guys. I'll try each two methods then I will replay with answer here.
i don't understand where you have the script file etc. could you please elaborate the above code a bit more? thx a lot!

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.