I have to send a URLRequest containing a JSON object as its http-body (which needs to be of type Data).
My JSON looks like this:
let json: [String : Any] = [
"to" : toDeviceToken, // String
"notification" : [
"title" : title, // String
"body" : body, // String
"icon" : icon // UIImage
],
"data" : {
// Add optional data here.
}
]
And I would like to convert this to type Data in order to put it into a URLRequest.
This
try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
always fails for some reason, maybe because it is a nested JSON object?
In addition, JSONSerialization.isValidJSONObject(json)
returns false.
When turning the UIImage object into a String object like this
UIImagePNGRepresentation(UIImage.appIcon!)?.base64EncodedString()
, I get an error saying 'Invalid type in JSON write (_SwiftValue)'
How would I do that?