0

I am communicating with js using Webview. I transfer into parameter api the received data again. But when Swift get an array from JS, Swift don't recognize it properly.

Where to Send Data from JS

var jsonData = {
  data,
  type : "fromjs"
};
alert(jsonData)
webkit.messageHandlers.sendFromJS.postMessage(jsonData);

Data Viewed by Alerts

{
    "type" = "fromjs",
    "data" = {
            "num": "one",
            "arr_data" : [{ "title": "test1", "val" : "testval1" },
                          { "title": "test2" , "val" : "testval2"}]
           }
}

I check the data received by Swift.

@available(iOS 8.0, *)
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
   if message.name == "sendFromJS" {
        let getdata = message.body as! NSDictionary
        print(getdata)

But Swift doesn't recognize the array properly.

{
    type = "fromjs",
    data = {
            "num": "one",
            "arr_data" : ({ "title": "test1", "val" : "testval1" },
                          { "title": "test2" , "val" : "testval2"})
           }
}

Swift recognizes square brackets as parentheses.

How can you solve this problem? I have to send the data that I received by putting it in the parameters as it is.

func apiCall(_ param: NSDictionary){
   let parameters: [String: AnyObject] = param["data"] as! [String : AnyObject]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers)

I've changed the type of data. But it's equally unrecognizable.

let getdata = message.body as! [String : Any]
 print(getdata)

//log
[
    type = "fromjs",
    data = {
            "num": "one",
            "arr_data" : ({ "title": "test1", "val" : "testval1" },
                          { "title": "test2" , "val" : "testval2"})
           }
]

To create json data in js

var data = {
  num: "one"
};
var data_arr = [];
data_arr.push({"title": "test1", "val" : "testval1"});
data_arr.push({"title": "test2" , "val" : "testval2"});
data.arr_data = data_arr;
var jsonData = {
  data,
  type : "fromjs"
};
alert(jsonData)
webkit.messageHandlers.sendFromJS.postMessage(jsonData);
11
  • @FahriAzimov I wrote down the data shown in the Swift log in the question. Is this correct? So why doesn't the parameters have an array? Commented Nov 15, 2019 at 1:18
  • Yes, it's correct, because you are printing the NSDictionary, if you print Swift dictionary, it will have square brackets ) Commented Nov 15, 2019 at 1:22
  • Sorry, my prev. comment was a little wrong, deleted it Commented Nov 15, 2019 at 1:22
  • Anyways, don't use Objective C types with Swift, it has it's own types, for the dictionary, it has Dictionary, or you can directly use [String: Any] as type and it will be a dictionary. Commented Nov 15, 2019 at 1:24
  • @FahriAzimov Do you mean to change the parameter type of the function to [String: Any]? Commented Nov 15, 2019 at 1:33

1 Answer 1

1

This is just a difference in the way that JSON and traditional NeXT property lists represent arrays.

Example

let json = """{ "a": [1, 2, 3] }"""
let jsonData = try! JSONSerialization.jsonObject(with: Data(json.utf8), options: [])
print("plist:", jsonData)
let jsonString = try! JSONSerialization.data(withJSONObject: d, options: [.prettyPrinted])
print("json:", String(data: jsonString, encoding: .utf8)!)  

Print Log

plist: {
    a =     (
        1,
        2,
        3
    );
}
json: {
  "a" : [
    1,
    2,
    3
  ]
}  

The property list renders the array using ( … ) but that’s just a rendering oddity. The actual objects are correct, as witnessed by the fact that I can turn them back into the some JSON I started with.

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

Comments

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.