I cannot use Alert inside response for request in swift 3

Hi, I am using the post and get the response method and its working, but there are two problems: 1- I cannot use the alert when receiving the response and 2 - I cannot get the api_token and Id in the response for the other option

here is the code i used to post ** i'm using local, but you can change url and check this **

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

      

** Remember that the first Alert works well - it is a warning that the application cannot connect to the server. But the problem is with the second warning, and when the user finished the text field and clicked the register button, the application will crash and the main thing is that even though the application will crash, the information will be sent to the server correctly **

+3


source to share


1 answer


You only need to execute the UI related code on the main thread.



DispatchQueue.main.async { 

    // Alert Controller Code Here

}

      

+3


source







All Articles