【问题标题】:Call javascript function in Objective-C with JavaScriptCore使用 JavaScriptCore 在 Objective-C 中调用 javascript 函数
【发布时间】:2015-10-08 07:12:36
【问题描述】:

我有一个 javascript 代码(来自annevk/webvtt):

var WebVTTParser = function () {
    this.parse = function (input, mode) {
        ...
    }
};

在 HTML 中我这样称呼它:

var pa = new WebVTTParser()
var r = pa.parse(input, mode)

现在我想在 Objective-C 中对此进行评估。我试过了:

[jsContext evaluateScript:script];

JSValue *parse = jsContext[@"WebVTTParser"][@"parse"];
NSLog("%@", [parse toString]); // undefined

JSValue *parsed = [parse callWithArguments:@[ input, mode ]];
NSLog("%@", [parsed toString]); // undefined

没有成功。 parse 未定义,因此 parsed 也未定义。

如果我使用这个:

JSValue *webVTTParser = jsContext[@"WebVTTParser"];
NSLog("%@", [webVTTParser toString]);

JSValue *parse = webVTTParser[@"parse"];
NSLog("%@", [parse toString]); // undefined

或者这个:

JSValue *webVTTParser = jsContext[@"WebVTTParser"];
NSLog("%@", [webVTTParser toString]);

JSValue *parserFunction = [webVTTParser callWithArguments:@[]]; // equivalent new WebVTTParser()?
NSLog(@"%@", [parserFunction toString]); // undefined

JSValue *parse = parserFunction[@"parse"];
NSLog("%@", [parse toString]); // undefined

然后webVTTParser 打印在控制台日志中,但其他所有内容仍未定义,因此我无法运行它。

如何从webVttParser 访问this.parse 功能?

【问题讨论】:

    标签: objective-c javascriptcore


    【解决方案1】:

    好的,看了JSValue的源码,我找到了解决办法。

    new WebVTTParser() 将是

    JSValue *webVTTParser = jsContext[@"WebVTTParser"];
    JSValue *parser = [webVTTParser constructWithArguments:@[]];
    

    来自苹果:

    /*!
    @method
    @abstract Invoke a JSValue as a constructor.
    @discussion This is equivalent to using the <code>new</code> syntax in JavaScript.
    @param arguments The arguments to pass to the constructor.
    @result The return value of the constructor call.
    */
    - (JSValue *)constructWithArguments:(NSArray *)arguments;
    

    并访问this.parse方法:

    JSValue *parsed = [parser invokeMethod:@"parse" withArguments:@[ input, mode ]];
    

    如:

    /*!
    @method
    @abstract Invoke a method on a JSValue.
    @discussion Accesses the property named <code>method</code> from this value and
                calls the resulting value as a function, passing this JSValue as the
                <code>this</code> value along with the specified arguments.
    @param method The name of the method to be invoked.
    @param arguments The arguments to pass to the method.
    @result The return value of the method call.
    */
    - (JSValue *)invokeMethod:(NSString *)method withArguments:(NSArray *)arguments;
    

    但是因为我要构造和调用几个方法,所以我没有使用那些复杂的Objective-C代码,而是在脚本中添加了自己的javascript函数:

    var parse = function(input, mode) {
        var pa = new WebVTTParser(),
            r = pa.parse(input, mode);
        ...
        return ...
    };
    

    并且只在 Obj-C 中调用这个函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多