JS与OC交互

#import

OC调用JS

JSContext *context = [[JSContext alloc] init]; // JSContext 是 JS运行环境

NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"jsFileName" ofType:@"js"];

NSString* jsContent = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];

//Native中 调 JS

JSValue *value = [context evaluateScript:jsContent];

//调用JS函数

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;

}

//传参数

JSValue *result = [sum callWithArguments:@[videoId]];

NSString* secondUrl = [result toString];

你可能感兴趣的:(JS与OC交互)