JavascriptCore:在JSExport中将javascript函数作为参数传递 [英] JavascriptCore: pass javascript function as parameter in JSExport

查看:156
本文介绍了JavascriptCore:在JSExport中将javascript函数作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavascriptCore是iOS7支持的新框架。我们可以使用JSExport协议将部分objc类暴露给JavaScript。

JavascriptCore is a new framework supported in iOS7. We can use the JSExport protocol to expose parts of objc class to JavaScript.

在javascript中,我尝试将函数作为参数传递。就像这样:

In javascript, I tried to pass function as parameter. Just like this:

function getJsonCallback(json) {
        movie = JSON.parse(json)
        renderTemplate()
}
viewController.getJsonWithURLCallback("", getJsonCallback)

在我的objc viewController中,我定义了我的协议:

In my objc viewController, I defined my protocol:

@protocol FetchJsonForJS <JSExport>
 - (void)getJsonWithURL:(NSString *)URL
               callback:(void (^)(NSString *json))callback;
 - (void)getJsonWithURL:(NSString *)URL
         callbackScript:(NSString *)script;
@end

在javascript中,viewController.getJsonWithURLCallbackScript工作,但是,viewController.getJsonWithURLCallback无效。

In javascript, viewController.getJsonWithURLCallbackScript works, however, viewController.getJsonWithURLCallback not work.

我在JSExport中使用了块有什么错误吗? thx。

Is there any mistake that I used block in JSExport? Thx.

推荐答案

问题是你已经将回调定义为采用NSString arg的Objective-C块但javascript没有知道如何处理这个并在你尝试评估viewController.getJsonWithURLCallback(,getJsonCallback)时产生异常 - 它认为第二个参数的类型是'undefined'

The problem is you have defined the callback as an Objective-C block taking a NSString arg but javascript doesn't know what to do with this and produces an exception when you try to evaluate viewController.getJsonWithURLCallback("", getJsonCallback) - it thinks the type of the second parameter is 'undefined'

相反,您需要将回调定义为javascript函数。

Instead you need to define the callback as a javascript function.

您可以使用JSValue在Objective-C中执行此操作。

You can do this in Objective-C simply using JSValue.

对于那里的其他读者,这是一个完整的工作示例(有异常处理):

For other readers out there, here's a complete working example (with exception handling):

TestHarnessViewController.h:

#import <UIKit/UIKit.h>
#import <JavaScriptCore/JavaScriptCore.h>

@protocol TestHarnessViewControllerExports <JSExport>
- (void)getJsonWithURL:(NSString *)URL callback:(JSValue *)callback;
@end

@interface TestHarnessViewController : UIViewController <TestHarnessViewControllerExports>
@end

TestHarnessViewController.m:
(如果使用复制/粘贴,删除evaluateScript中的换行符 - 为了清楚起见添加了这些换行符):

TestHarnessViewController.m: (if using copy/paste, remove the newlines inside the evaluateScript - these were added for clarity):

#import "TestHarnessViewController.h"

@implementation TestHarnessViewController {
    JSContext *javascriptContext;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    javascriptContext  = [[JSContext alloc] init];
    javascriptContext[@"consoleLog"] = ^(NSString *message) {
        NSLog(@"Javascript log: %@",message);
    };
    javascriptContext[@"viewController"] = self;

    javascriptContext.exception = nil;
    [javascriptContext evaluateScript:@"
        function getJsonCallback(json) {
            consoleLog(\"getJsonCallback(\"+json+\") invoked.\");
            /* 
            movie = JSON.parse(json); 
            renderTemplate(); 
            */
        } 

        viewController.getJsonWithURLCallback(\"\", getJsonCallback);
    "];
    JSValue *e = javascriptContext.exception;
    if (e != nil && ![e isNull])
        NSLog(@"Javascript exception occurred %@", [e toString]);
}

- (void)getJsonWithURL:(NSString *)URL callback:(JSValue *)callback {
    NSString *json = @""; // Put JSON extract from URL here
    [callback callWithArguments:@[json]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这篇关于JavascriptCore:在JSExport中将javascript函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆