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

Viewed 1357

Hi I am using post and get response method and its work well but there are two problems : 1- I can't use alert when response is get and 2 - I can't get api_token and Id in the response to the another variation

here is the code that I used for post **I am using local but you can change the url and test 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 Working well - this Alert is for times that the app can't connect to the server But the problem is the second Alert and when the user completed the text field and tap sign up button the app will crash and the Important thing is that despite the app will crash but the informations will send to the server correctly **

1 Answers
Related