我无法在 swift 3 中对请求使用 Alert inside response

I can't use Alert inside response for request in swift 3

您好,我正在使用 post 并获得响应方法,它运行良好,但有两个问题: 1- 收到响应时我无法使用警报 2 - 我无法在对另一个变体

的响应中得到 api_token 和 Id

这是我用于 post 的代码 **我正在使用本地,但您可以更改 url 并测试它 **

 var request = URLRequest(url: URL(string: "http://172.16.15.137:8888/TheoryTipo/public/api/register")!)
                     request.httpMethod = "POST"
                     let postString = "name=\(usernameforsignup.text!)&email=\(emailforsignup.text!)&password=\(passwordforsignup.text!)&tel=\(phonenumberforsignup.text!)"

                    print(postString)

                     request.httpBody = postString.data(using: .utf8)
                     let task = URLSession.shared.dataTask(with: request) { data, response, error in
                     guard let data = data, error == nil else {
                        // check for fundamental networking error
                     print("error=\(error)")

                        let alertController = UIAlertController(title: "Error", message: "can't Connect to the server", preferredStyle: UIAlertControllerStyle.alert)

                        let okAction = UIAlertAction(title: "retry", style: UIAlertActionStyle.default)
                        {
                            (result : UIAlertAction) -> Void in
                        }
                        alertController.addAction(okAction)
                        self.present(alertController, animated: true, completion: nil)
                        alertController.view.tintColor = UIColor.red
                        alertController.view.backgroundColor = UIColor.red
                        alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width

                     return
                     }

                     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                     print("statusCode should be 200, but is \(httpStatus.statusCode)")
                     print("response = \(response)")
                     }

                     let responseString = String(data: data, encoding: .utf8)
                     print("responseString = \(responseString)")


                        print("OK")
                        let alertController = UIAlertController(title: "Complete", message: "Complete Sign Up" , preferredStyle: UIAlertControllerStyle.alert)


                        let okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default)
                        {

                            (result : UIAlertAction) -> Void in

                        }
                            alertController.addAction(okAction)

                        self.present(alertController, animated: true, completion: nil)
                        alertController.view.tintColor = UIColor(red: 50/255, green: 118/255, blue: 43/255, alpha: 1.0)
                        alertController.view.backgroundColor = UIColor(red: 50/255, green: 118/255, blue: 43/255, alpha: 1.0)
                        alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width



                     }
                     task.resume()

** 请记住,第一个警报运行良好 - 此警报适用于应用程序无法连接到服务器的时间但问题是第二个警报,当用户完成文本字段并点击注册按钮时该应用程序将崩溃,重要的是尽管该应用程序将崩溃,但信息将正确发送到服务器 **

您只需要在主线程上执行UI相关代码。

DispatchQueue.main.async { 

    // Alert Controller Code Here

}