So I am working on a application that logs into something I am also using alamofire. Now what I want to do is to have it go to a new view when the response code for the connection is 200 (success) and then go to a different windows when the response code is 204 (failure) I am writing this in swift 3.
Here is my current IBAction
@IBAction func loginBtn(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "login", sender: isvalid200())
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "error", sender: isinvalid())
Now I know thats not how it is suppose to be done but it's what I have in mind. Also, here is my error handling code.
func isvalid200() {
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)")
.validate(statusCode: 200..<200) // if error is 200
.validate(contentType: ["application/json"])
}
func isinvalid() {
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)")
.validate(statusCode: 204..<204) // if error is 204
.validate(contentType: ["application/json"])
}
As for the error I get it is this
MyAnime[7446:265163] >'s window is not equal to 's view's window!
(without the self.dimiss but with it, it does work however it doesn't stay go to a specific windows based on the response code I.E 200 or 204.
Any help would be great!
Try something like this which switches on the statusCode
@IBAction func loginBtn(_ sender: Any) {
Alamofire.request("https://myanimelist.net/api/account/verify_credentials.xml/\(user)/\(pass)").responseJSON { [weak self] response in
guard let strongSelf = self, let resp = response.response else { return }
switch resp.statusCode {
case 200:
DispatchQueue.main.async {
strongSelf.performSegue(withIdentifier: "login", sender: strongSelf)
}
case 204:
DispatchQueue.main.async {
strongSelf.performSegue(withIdentifier: "error", sender: strongSelf)
}
default:
//Some other status code
}
}
}
To give some explanation on what's going on lets break it down by step: