参考: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
本文介绍如何在Objective-C中调用JavaScript函数,包括使用_webView.stringByEvaluatingJavaScriptFromString方法执行简单的JavaScript函数调用及利用JavaScriptCore框架实现更复杂的功能。文中还提供了一个示例项目链接。
9677

被折叠的 条评论
为什么被折叠?



