Objective-C
Objective-C
Call a JavaScript Function Passing an Object Argument
See more JavaScript Examples
Demonstrates how to call a JavaScript function with an argument that is an object.Chilkat Objective-C Downloads
#import <CkoStringBuilder.h>
#import <CkoJs.h>
#import <CkoJsonObject.h>
#import <CkoJsonArray.h>
BOOL success = NO;
// This is the JavaScript function we'll call:
// function describeCar(car) {
// console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
// }
CkoStringBuilder *sbScript = [[CkoStringBuilder alloc] init];
[sbScript Append: @"function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }"];
CkoJs *js = [[CkoJs alloc] init];
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.EmitCompact = NO;
// Call Eval to add the function to the context's global object
success = [js Eval: sbScript result: result];
if (success == NO) {
// Examine the result for an exception.
NSLog(@"%@",[result Emit]);
// Also examine the LastErrorText.
NSLog(@"%@",js.LastErrorText);
return;
}
// ------------------------------------------------------------------------------
// Call the function describeCar(car)
CkoJsonObject *funcCall = [[CkoJsonObject alloc] init];
funcCall.EmitCompact = NO;
// Create JSON specifying the function name and arguments
// In this case, there is only 1 argument, and it is an object.
// {
// "name": "describeCar",
// "args": [
// {
// "make": "Toyota",
// "model": "Corolla",
// "year": 2022
// }
// ]
// }
[funcCall UpdateString: @"name" value: @"describeCar"];
// Create the JSON object that is the argument.
CkoJsonObject *arg = [[CkoJsonObject alloc] init];
[arg UpdateString: @"make" value: @"Toyota"];
[arg UpdateString: @"model" value: @"Corolla"];
[arg UpdateInt: @"year" value: [NSNumber numberWithInt: 2022]];
// Create the arguments array.
CkoJsonArray *argsArray = [[CkoJsonArray alloc] init];
[argsArray AddObjectCopyAt: [NSNumber numberWithInt: 0] jsonObj: arg];
// Add the "args" array to the funcCall.
[funcCall AppendArrayCopy: @"args" jarr: argsArray];
NSLog(@"%@",[funcCall Emit]);
success = [js CallFunction: funcCall result: result];
if (success == NO) {
// Examine the result for an exception.
NSLog(@"%@",[result Emit]);
// Also examine the LastErrorText.
NSLog(@"%@",js.LastErrorText);
return;
}
NSLog(@"%@",[result Emit]);
// The describeCar JavaScript function returns nothing.
// Therefore, the result is "undefined".
// {
// "type": "undefined",
// "value": "undefined"
// }
// However, the function emitted text to the console.
CkoStringBuilder *sbOut = [[CkoStringBuilder alloc] init];
[js ConsoleOutputSb: sbOut];
NSLog(@"%@",[sbOut GetAsString]);
// Output:
// This is a 2022 Toyota Corolla.
// -----------------------------------------------------------
// Note: If the object argument is simple, this is an alternative
// and simpler way of creating the funcCall:
[funcCall Clear];
[funcCall UpdateString: @"name" value: @"describeCar"];
[funcCall UpdateString: @"args[0].make" value: @"Toyota"];
[funcCall UpdateString: @"args[0].model" value: @"Corolla"];
[funcCall UpdateInt: @"args[0].year" value: [NSNumber numberWithInt: 2022]];
NSLog(@"%@",[funcCall Emit]);