1
I have a method that does the login and password checking using REST
, but after reading I need to return a value Bool
to validate whether the login was done.
I don’t 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 went right or wrong, and only then it loads this information.
Look at:
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
}