我无法在swift 3中使用Alert inside响应请求

时间:2017-05-21 04:57:05

标签: ios swift3 http-post uialertcontroller

您好我正在使用post和get response方法及其工作正常但有两个问题: 1-响应得到时我不能使用警报2 - 我在另一个变体的响应中无法获得api_token和Id

这是我用于发布的代码 **我正在使用本地但您可以更改网址并测试此**

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

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

1 个答案:

答案 0 :(得分:4)

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

DispatchQueue.main.async { 

    // Alert Controller Code Here

}