I have a method that does the verification of login and password using REST
, but after reading I need to return a Bool
value to validate if the login was done.
I do not know if I'm doing it the best way, anyway the problem is this: it finishes executing the function before answering me if the login was successful or wrong, and then it loads this information. >
See:
func logarNoSistema(email: String, senha: String) -> Bool
{
var retorno: Bool = false
let urlPath = "http://meuendereco/api/user/login?code=xxxxxx&email=\(email)&senha=\(senha)"
let url = NSURL(string: urlPath)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
if (error != nil) {
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
if (err != nil) {
println("JSON Error \(err!.localizedDescription)")
}
let erro : Bool = jsonResult["erro"] as! Bool
println(erro)
println(jsonResult)
//AQUI EU FAÇO A VERIFICAÇÃO SE O LOGIN DEU CERTO E SETO O RETORNO
dispatch_async(dispatch_get_main_queue(), {
})
})
jsonQuery.resume()
return retorno
}