, Objective-C NSString arg, javascript , , , viewController.getJsonWithURLCallback("", getJsonCallback) - , undefined '
javascript.
Objective-C, JSValue.
( ):
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:
( copy/paste, - ):
#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 = @"";
[callback callWithArguments:@[json]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end