I am using a rest API for logging in I simply send a request with the user's email and password . If they match in the database then I return some Json and swift parses it . That part is working correctly what I am trying to do now is figure out how I can alert a user if that the email and password don't match. So far I got this
@IBAction func Login_Action(_ sender: Any) {
var check = 0
let url:URL = URL(string:ConnectionString+"login")!
let Email = email.text!
let Password = password.text!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let parameter = "email=\(Email)&password=\(Password)"
request.httpBody = parameter.data(using: String.Encoding.utf8)
session.dataTask(with:request, completionHandler: {(data, response, error) in
if error != nil {
// print(error)
} else {
do {
// if the call is successful then it goes through here otherwise it skips it
check = 1
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
}
DispatchQueue.main.async {
// send them to member page
let next = self.storyboard?.instantiateViewController(withIdentifier: "NextView")
self.present(next, animated: true, completion: nil)
}
}
} catch let error as NSError {
print(error)
if check == 0 {
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
}
}
}
// If the call is unsuccessful then show message
return
}).resume()
/*
print(check)
if check == 0 {
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
print("incorrect")
}*/
}
I am using the check variable as the flag it's default is 0 . Which means that the email/password combination does not work and if the email/password combination works then I set it to 1 . The issue is that it takes about 5 or 7 seconds to display the Incorrect email and/or password message if the user supplied the wrong credentials . When I check the call response time it comes back immediately yet it takes a good 7 seconds for the label to show . If I put that same logic outside the .resume then it appears instantly however the message is always incorrect email and password whether or not the check is 0 or 1 any suggestions would be better