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 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"

PFB the code snippet for your reference used in the project.

Native code calling JS Function:

     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:

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);
        });

        apiRequest.addEventListener('error', function (e) {            
            // return [];
            reject("ERROR");

        });
    });
}

0

There are 0 best solutions below