#import <JavaScriptCore/JavaScriptCore.h>
OC調(diào)用JS
JSContext *context = [[JSContext alloc] init]; // JSContext 是 JS運行環(huán)境
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"jsFileName" ofType:@"js"];
NSString* jsContent = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];
//Native中 調(diào) JS
JSValue *value = [context evaluateScript:jsContent];
//調(diào)用JS函數(shù)
JSValue *sum = context[@"get_video_url"];
// js文件中方法為
function get_video_url(aid) {
var a = "www" + Date.parse(new Date);
var token = w(a);
return "http://www.baidu.com" + aid ?+ token;
}
//傳參數(shù)
JSValue *result = [sum callWithArguments:@[videoId]];
NSString* secondUrl = [result toString];