Skip to content Skip to sidebar Skip to footer

Calling Javascript From Objective-c Code

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 no

Solution 1:

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.

Solution 2:

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.

Post a Comment for "Calling Javascript From Objective-c Code"