admin管理员组文章数量:1402786
JavascriptCore is a new framework supported in iOS7. We can use the JSExport protocol to expose parts of objc class to JavaScript.
In javascript, I tried to pass function as parameter. Just like this:
function getJsonCallback(json) {
movie = JSON.parse(json)
renderTemplate()
}
viewController.getJsonWithURLCallback("", getJsonCallback)
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
In javascript, viewController.getJsonWithURLCallbackScript works, however, viewController.getJsonWithURLCallback not work.
Is there any mistake that I used block in JSExport? Thx.
JavascriptCore is a new framework supported in iOS7. We can use the JSExport protocol to expose parts of objc class to JavaScript.
In javascript, I tried to pass function as parameter. Just like this:
function getJsonCallback(json) {
movie = JSON.parse(json)
renderTemplate()
}
viewController.getJsonWithURLCallback("", getJsonCallback)
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
In javascript, viewController.getJsonWithURLCallbackScript works, however, viewController.getJsonWithURLCallback not work.
Is there any mistake that I used block in JSExport? Thx.
Share Improve this question asked Mar 24, 2014 at 10:45 liaojinxingliaojinxing 631 silver badge6 bronze badges1 Answer
Reset to default 7The 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'
Instead you need to define the callback as a javascript function.
You can do this in Objective-C simply using JSValue.
For other readers out there, here's a plete 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: (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
本文标签: iosJavascriptCore pass javascript function as parameter in JSExportStack Overflow
版权声明:本文标题:ios - JavascriptCore: pass javascript function as parameter in JSExport - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744368108a2602883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论