I uploaded an image using almofire and it is being uploaded to the correct path I require.
However, I need to get some of the responses that are in my PHP backend code into my swift such as the filepath
.
An image to make my question more clear and precise about what I want to get from the .responseJSON
In the image below is my response from the PHP code, I want to get the value of the filepath in swift. How can I achieve that?
Here is my code:
PHP:
<?php
if (empty($_FILES["image"])) {
$response = array("error" => "nodata");
}
else {
$response['error'] = "NULL";
$filename = uniqid() . ".jpg";
if (move_uploaded_file($_FILES['image']['tmp_name'], "../propertyImages/" . $filename)) {
$response['status'] = "success";
$response['filepath'] = "https://example.com/WebService/propertyImages/" . $filename;
$response['filename'] = "".$_FILES["file"]["name"];
} else{
$response['status'] = "Failure";
$response['error'] = "".$_FILES["image"]["error"];
$response['name'] = "".$_FILES["image"]["name"];
$response['path'] = "".$_FILES["image"]["tmp_name"];
$response['type'] = "".$_FILES["image"]["type"];
$response['size'] = "".$_FILES["image"]["size"];
}
}
echo json_encode($response);
?>
Swift Code:
self.imageData = propImage.image!.jpegData(compressionQuality: 0.5)!
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(self.imageData!, withName: "image" , fileName: "file.jpg", mimeType: "image/jpeg")
},
to:"https://example.com/WebService/api/uploadPropImage.php", method: .post , headers: headers)
.responseJSON { resp in
//let responseString: String = String(data: self.imageData!, encoding: .utf8)!
print(resp) //this prints all the responses from the PHP code, my problem is how do i get a specific response, such as the filepath only and so on?
}
EDIT:
I tried some of the solutions, this one seems to be the one that will work but still gives an error reading
"No exact matches in call to class method 'jsonObject'"
Updated code:
AF.upload(multipartFormData: { multipartFormData in multipartFormData.append(self.imageData!, withName: "image" , fileName: "file.jpg", mimeType: "image/jpeg")}, to:"https://example.com/WebService/api/uploadPropImage.php", method: .post , headers: headers).responseJSON {
result in
do{
if let jsonResults = try JSONSerialization.jsonObject(with: result, options: []) as? [String: Any] { //error in this line
let filePath = jsonResults["filepath"] as? String
}
}catch{
print("ERROR")
}
2
Answers
Then just decode your response:
The value of the
responseJSON
block, is aResult
. It’s a basic concept used frequently, so you need to learn how do handle it. Common practice is then to use aswitch
.}
Now, it might be better to use a
Codable
struct to parse your response and callresponseDecodable()
instead of usingresponseJSON()
which will useJSONSerialization
, method which is by the way deprecated and will be removed in next Alamofire major release.