How to Extract JSON Response Values Coming From PHP Backend in Swift iOS

310 Views Asked by At

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?

enter image description here

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

There are 2 best solutions below

1
AlexM On BEST ANSWER

Then just decode your response:

if let jsonResults = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
    let filePath = jsonResults["filepath"] as? String // here is your value
}
0
Larme On

The value of the responseJSON block, is a Result. It's a basic concept used frequently, so you need to learn how do handle it. Common practice is then to use a switch.

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 { result in
    switch result {
        case .success(let json):
            guard let dictionary = json as? [String: Any] else { print("Response JSON is not a dictionary"); return }
            guard let filePath = json["filePath"] as? String else { print("filePath key is not present in "\(json)" or is not a String");  return }
            print("Filepath: \(filePath)")
        case .failure(let error):
            print("Error: \(error)")
    }

}

Now, it might be better to use a Codable struct to parse your response and call responseDecodable() instead of using responseJSON() which will use JSONSerialization, method which is by the way deprecated and will be removed in next Alamofire major release.