1

I have one javascript function which return multiple values. I am trying to call that function in my iOS app using Webview. I have tried searching for similar question but found nothing. Check the code below. FYI single return value works fine and I am calling this javascript after "webview did finish" delegate called.

//Javascript function
function listABCs() constant returns (uint noOfABCs, address[] retABCAddresses) {
    return (abcAddresses.length, abcAddresses);
}

//iOS code
let script = "getAbcList()"
wkWebView?.evaluateJavaScript(script, completionHandler: { (response: Any?, error:Error?) in
       print("\n Error \(String(describing: error))")
       let res = response as? String
       print("Response = \(String(describing: response))")
})

Thanks in advance.

2 Answers 2

1

I solved this by returning a dictionary instead of a string and then using the dictionary in iOS to retrieve the values.

Javascript code:

function customDictFrom(val1, val2) {

    var data_to_pass = {
        'val1': val1,
        'val2': val2
    };

    return data_to_pass;
}

function listABCs() constant returns (uint noOfABCs, address[] retABCAddresses) {
    var data_to_pass = customDictFrom(abcAddresses.length, abcAddresses);
    return data_to_pass;
}

Swift (Native) Handling:

let script = "getAbcList()"
wkWebView?.evaluateJavaScript(script, completionHandler: { (response: Any?, error:Error?) in
       print("\n Error \(String(describing: error))")
       let res = response as? [String: Any] //Your response is here as a dictionary
       print("Response = \(String(describing: response))")
})

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply but I suggested the same approach to the server team but what they want is different values separately not inside the dictionary. You are correct in the approach but this is not something I am looking for.
They can call a method and what you do with that method and how you send it to your native layer is upto you. Server guy doesn't need to know about it.
0

You can add a boolean once you received your value the first time set that to true

//Create a variable received response
var receivedResponse = false

let script = "getKycList()"
wkWebView?.evaluateJavaScript(script, completionHandler: { (response: Any?, error:Error?) in
   print("\n Error \(String(describing: error))")
   if !receivedResponse {
      if let res = response as? String {
          // use the response now
          print("Response = \(res)")

         // set the receivedResponse to true
         receivedResponse = true
      }

   }

})

3 Comments

I want to get both return values in separate object and use them. This doesn't work.
OK. Can you post some javascript example what are you trying to return. I think in that case your javascript function should return an array so you can get the values from that array
javascript function already added to the question. And is there any other way except array to get multiple values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.