I am developing a MacOS app that has a login page. When the user pressed the login button i need to send a post request and if the response is code is 200 then i need to preform a segue.
I am running into an issue where the segue is occurring no matter what i try
I have tried using the IBAction for a button then calling preform segue however that resulted in a thread problem. I have now put everything in shouldPerformSegue
override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool {
if emailTextField.stringValue.isEmpty || passwordTextField.stringValue.isEmpty {
instructionText.stringValue = "Email and Password Required"
return false
}
let emailPassword = "email="+emailTextField.stringValue+"&password="+passwordTextField.stringValue
print("before post")
let data = emailPassword.data(using: String.Encoding.ascii, allowLossyConversion: false)
let url = URL(string: "http://127.0.0.1:50896/api/v1/auth")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = data
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("error: \(error)")
} else {
if let response = response as? HTTPURLResponse {
print("statusCode: \(response.statusCode)")
}
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("data: \(dataString)")
}
}
}
task.resume()
return true
}
I would like to complete the post request, check response code then preform segue if the code is 200