I am calling JavaScript function written in local JS file from application’s native iOS code (.m file) as below.
[vc.webViewEngine evaluateJavaScript:javascript completionHandler:^(id result, NSError *error) {
The JavaScript function accepts string as the argument and return string/array in the response.
The function (Function1) invoked successfully and return the value which is received from third party API returned by another function (Function2) call internally in JavaScript file. But when the Function1 function return the value to native code, the “result” , is always coming as “nil” and error object has a value as “Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type"
Below are the code snippet for your reference used in the project.
Native code calling JS Function:
Javascript Code:
[vc.webViewEngine evaluateJavaScript:javascript completionHandler:^(id result, NSError *error) {
The JavaScript function accepts string as the argument and return string/array in the response.
The function (Function1) invoked successfully and return the value which is received from third party API returned by another function (Function2) call internally in JavaScript file. But when the Function1 function return the value to native code, the “result” , is always coming as “nil” and error object has a value as “Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type"
Below are the code snippet for your reference used in the project.
Native code calling JS Function:
Code Block NSString *jsFunc = [NSString stringWithFormat:@"Function1('%@','%@','%@','%@','%@','%@','%@')", param1, param2, param3, param4, param5]; [vc.webViewEngine evaluateJavaScript:jsFunc completionHandler:^(id result, NSError *error) { if (error == nil) { if (result != nil) { NSLog(@"Succes"); } } else { NSLog(@"Error== %@",[error localizedDescription]); finished = YES; }
Javascript Code:
Code Block async function Function1(param1, param2, param3, param4, param5) { var result = []; try { result = await Function2(param1, param2, param3, param4, param5) } catch (e) { console.log("Exception == " + e); } return result; } function Function2(param1, param2, param3, param4, param5) { return new Promise(function (resolve, reject) { ... ...some code to call third party API ... var self = this; apiRequest.addEventListener('response', function (e) { var response = JSON.parse(e.detail.response); var returnString = JSON.stringify(response); var payload = []; payload[0] = response.obj1 payload[1] = response.obj2 payload[2] = response.obj3 payload[3] = response.obj3 resolve(payload); }); applePayencryptRequest.addEventListener('error', function (e) { // return []; reject("ERROR"); }); }); }