如何使用 SWIFT POST 从多个视图控制器到服务器 JSON 的数据

How to POST data from multiple view controllers to server with JSON using SWIFT

我需要帮助来合并从第一个VC、第二个VC和第三个VC收集的数据,并将第四个VC.[=16=中的数据序列化]

这个 link 对一个 VC 有帮助,但我只需要向服务器发送 JSON 数据的一个文件。

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

另一种方法是将字典数组从第一个VC、第二个VC和第三个VC传递到第四个VC和第四个[=38] =] 将字典转换为 JSON。但是我不知道该怎么做。

我使用了上面 link 中提供的答案中的格式,但如果您需要更多信息,我很乐意合作。谢谢!

PS。请给我有用的评论,这将以任何方式提供帮助。我需要代码,而不是像我自己做研究那样的反馈,因此我已经为此强调了将近一个月了。

这是 UserDefault 键

 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)"
    }

这是我的 JSON 代码

@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)

}

我首先将它们存储在一个变量中解决了这个问题

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

然后我把它们放在一个参数中就完成了。太明显了,我不敢相信我没有早点解决它

@IBAction func save(_ sender: Any){

    let parameters = ["Tax Year": TaxYear, "Title": TITLE, "first-name": GN, "sur-name": LN]