admin管理员组文章数量:1313121
I've been playing around with the new iOS 7/Mavericks JavascriptCore bridge, trying to get JS functions on Objective-C as blocks.
The JavascriptCore header files states that this is possible as long as every argument is supported, but trying this:
JSContext *context = [[JSContext alloc] init];
context[@"Log"] = ^(NSString *message){NSLog(@"%@", message);};
context[@"BlockTest"] = ^(void (^blockTest)(NSString* blockString)){
NSLog(@"Calling Block Test");
blockTest(@"STRINGGGGG");
};
[context evaluateScript:@"BlockTest( function(dataString){Log('JS '+dataString);} )"];
raises the following JS exception:
TypeError: '[object NSBlock]' is not a function (evaluating 'BlockTest( function(dataString){Log('JS '+dataString);} )')
Any ideas on what's wrong with my test code?
I've been playing around with the new iOS 7/Mavericks JavascriptCore bridge, trying to get JS functions on Objective-C as blocks.
The JavascriptCore header files states that this is possible as long as every argument is supported, but trying this:
JSContext *context = [[JSContext alloc] init];
context[@"Log"] = ^(NSString *message){NSLog(@"%@", message);};
context[@"BlockTest"] = ^(void (^blockTest)(NSString* blockString)){
NSLog(@"Calling Block Test");
blockTest(@"STRINGGGGG");
};
[context evaluateScript:@"BlockTest( function(dataString){Log('JS '+dataString);} )"];
raises the following JS exception:
TypeError: '[object NSBlock]' is not a function (evaluating 'BlockTest( function(dataString){Log('JS '+dataString);} )')
Any ideas on what's wrong with my test code?
Share Improve this question edited Oct 17, 2013 at 10:08 Jorge Cohen asked Oct 17, 2013 at 9:52 Jorge CohenJorge Cohen 1,52210 silver badges34 bronze badges 1- 1 The answer with 7 upvotes should be accepted as correct answer – prabodhprakash Commented Dec 18, 2016 at 20:45
2 Answers
Reset to default 8I know that you've accepted the answer that support for this was removed (and that this is an old question), but you can pass a JS function as a block to Objective-C in the JavaScriptCore framework introduced with iOS7/Mavericks - your code just needs to change slightly how you declare and invoke the block:
JSContext *context = [[JSContext alloc] init];
context[@"Log"] = ^(NSString *message){NSLog(@"%@", message);};
context[@"BlockTest"] = ^(JSValue *blockTest){
NSLog(@"Calling Block Test");
[blockTest callWithArguments:@[@"STRINGGGGG"]];
};
[context evaluateScript:@"BlockTest( function(dataString){Log('JS '+dataString);} )"];
Seems like support for this was removed and documentation not updated:
http://trac.webkit/changeset/144489
本文标签:
版权声明:本文标题:javascript - JavaScriptCore: Trying to pass JS function as block to Objective-C, Getting 'TypeError' Exception - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741939452a2406053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论