Hi i have a beginner question, where i cant find a good solution for in Swift 3. I hope someone is able to hlep.
i have the following code that will check a rest api if the user credentials are valid or not. I want it to wait for the resquest is finished and then return true or false. now it is being send async.
Also any improvement in the way i check the JSON value's would be welcome too.
func CheckUsernamePassword(username :String ,code:String )-> bool {
var validCredentials = false
let urlString = "\(self.baseurl)/accounts/validateusernamepassword.json?username=\(username)&password=\(code)&api_key=\(self.api_key)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print("Error URLSession : \(error!)")
validCredentials = false
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
if parsedData["validated"] != nil {
if "\(parsedData["validated"]!)" == "1" {
print("Login credentials are correct")
validCredentials = true
}else {
print("Login credentials are not correct")
print("\(parsedData["validated"]!)")
print("\(parsedData["message"]!)")
validCredentials = false
}
}else{
print("Json Parse error: \(parsedData)")
validCredentials = false
}
} catch let error as NSError {
print("Error Parsing Json \(error)" )
validCredentials = false
}
}
}.resume()
return validCredentials
}
You cannot return something from an asynchronous task as a return value.
Do not wait, use a completion handler:
Replace the signature of the method (the name is supposed to start with a lowercase letter) with
Delete the lines
var validCredentials = false
andreturn validCredentials
Replace all occurrences of
validCredentials = false
withcompletion(false)
andvalidCredentials = true
withcompletion(true)
.Call the method