0

I need help with combining data collected from firstVC, secondVC, and thirdVC and serializing those in the fourthVC.

This link helps with one VC but I have to send only ONE FILE of JSON DATA to the server.

How to create and send the json data to server using swift language

The other method is passing a dictionary array from firstVC, secondVC, and thirdVC to the fourthVC and from the fourthVC convert the dictionaries into JSON. But i don't know how to do that.

I used the format from the answer provided in the link above, but if you need additional info, I will gladly cooperate. Thanks!

PS. Please give me useful comments that will help in any way. I need the code and not feedbacks like doing my own research and such cause I have been stressing about this for nearly a month now.

This is the UserDefault keys

 if let AC = UserDefaults.standard.value(forKey: "Acc") as? String {
        labeltext.text = "\(AC)"
    }

    if let TY = UserDefaults.standard.value(forKey: "Taxyear") as? String {
        taxtext.text = "\(TY)"
    }
    if let BB = UserDefaults.standard.value(forKey: "Bsb") as? String {
        bsbtext.text = "\(BB)"
    }

Here is my JSON code

@IBAction func save(_ sender: Any){
    typealias JSONDictionary = [String:Any]

    let parameters = ["BankAccountNumber": "Acc", "Tax Year": "Taxyear", "my-bsb": "Bsb"]

    let url = URL(string: "https://server:port/")! //change the url

    //create the session object
    let session = URLSession.shared

    //now create the URLRequest object using the url object
    var request = URLRequest(url: url)
    request.httpMethod = "POST" //set http method as POST


   let valid = JSONSerialization.isValidJSONObject(parameters) // true

    print (valid)

    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body

    } catch let error {
        print(error)
    }
      request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //        create dataTask using the session object to send data to the server
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }
        guard let data = data else {
            return
        }

        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print(json)
                // handle json...
            }

        } catch let error {
            print(error.localizedDescription)
        }
    })
    task.resume()

    let alertMessage = UIAlertController(title: "Saved!", message: "We have recorded your information", preferredStyle: UIAlertControllerStyle.alert)

    let action = UIAlertAction(title:"Okay", style: UIAlertActionStyle.default, handler: nil)

    alertMessage.addAction(action)

    self.present(alertMessage, animated: true, completion: nil)

}
8
  • You pass the data from VC1 to VC2, you can create a property in VC2 and when you push from VC1 to VC2 set that property value, and from VC2 to VC3 also do the same till VC4. What is the problem ? Commented Aug 6, 2018 at 5:20
  • are you able to collect data from all viewControllers? If yes, can you share that along with the JSON structure you want to post? Commented Aug 6, 2018 at 5:20
  • Let the others to know about your key value structure to prepare data in json, so they can help. Commented Aug 6, 2018 at 5:30
  • @vivekDas Thanks for replying! I can pass data by using userdefaults. But I don't know how I can convert the keys into JSON. So I am open to any other alternatives Commented Aug 6, 2018 at 6:16
  • @Kamran Thanks for your suggestion. I've updated my question. I passed the values through using userdefaults Commented Aug 6, 2018 at 6:18

1 Answer 1

0

I solved it by first storing them in a variable

var TITLE = UserDefaults.standard.value(forKey: "Title")
var GN = UserDefaults.standard.value(forKey: "GivenNames")
var LN = UserDefaults.standard.value(forKey: "LastName")

Then I placed them in a parameter and that's done. It was so obvious that I can't believe I didn't solve it sooner

@IBAction func save(_ sender: Any){

    let parameters = ["Tax Year": TaxYear, "Title": TITLE, "first-name": GN, "sur-name": LN]
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.