ios - JavascriptCore: pass javascript function as parameter in JSExport -


javascriptcore new framework supported in ios7. can use jsexport protocol expose parts of objc class javascript.

in javascript, tried pass function parameter. this:

function getjsoncallback(json) {         movie = json.parse(json)         rendertemplate() } viewcontroller.getjsonwithurlcallback("", getjsoncallback) 

in objc viewcontroller, defined 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 mistake used block in jsexport? thx.

the problem have defined callback objective-c block taking nsstring arg javascript doesn't know , produces exception when try evaluate viewcontroller.getjsonwithurlcallback("", getjsoncallback) - thinks type of second parameter 'undefined'

instead need define callback javascript function.

you can in objective-c using jsvalue.

for other readers out there, here's 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: (if using copy/paste, remove newlines inside evaluatescript - these added 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 url here     [callback callwitharguments:@[json]]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  @end 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -