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)

}
booak55
  • 111
  • 2
  • 11
  • 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 ? – vivekDas Aug 06 '18 at 05: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? – Kamran Aug 06 '18 at 05:20
  • Let the others to know about your key value structure to prepare data in json, so they can help. – vaibhav Aug 06 '18 at 05: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 – booak55 Aug 06 '18 at 06:16
  • @Kamran Thanks for your suggestion. I've updated my question. I passed the values through using userdefaults – booak55 Aug 06 '18 at 06:18
  • @vaibhav okay thank you for your suggestion! I have done it. – booak55 Aug 06 '18 at 06:18
  • You know how to get the values from `UserDefaults` so inside `save` method you can get those values as you did to show on labels. Your post request looks fine. Only the alert message does not look ok, It should be inside the `completionHandler` after this line `guard error == nil else { return }`. Can you let us know what exactly is the issue now? – Kamran Aug 06 '18 at 06:25
  • @Kamran but it wouldn't work when I use the key (etc "Acc", "Taxyear"). When i POST it, it sends me a string Acc and Taxyear instead of the data that is saved – booak55 Aug 07 '18 at 00:31

1 Answers1

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]
booak55
  • 111
  • 2
  • 11