How to call JavaScript Function in objective C

参考:http://stackoverflow.com/questions/14334047/how-to-call-javascript-function-in-objective-c

[_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
NSString* returnValue =[theWebView stringByEvaluatingJavaScriptFromString:@"myFunction('pass your parameter')"];


One can use JavaScriptCore framework to run JavaScript code from Objective-C.
#import <JavaScriptCore/JavaScriptCore.h>
JSContext *context = [[JSContext alloc] init];
[context evaluateScript: @"function greet(name){ return 'Hello, ' + name; }"];
JSValue *function = context[@"greet"];
JSValue* result = [function callWithArguments:@[@"World"]];
[result toString]; // -> Hello, World

Here is a demo:
https://github.com/evgenyneu/ios-javascriptcore-demo

posted @ 2016-04-22 18:40  qike  阅读(156)  评论(0编辑  收藏  举报