斯威夫特 3 / 4
Metin Say 的回答转换为 Swift。
static func signIn(username: String, password: String, onSuccess: @escaping ([PFObject]) -> (), onError: @escaping (String) -> ()) {
let query = PFQuery(className: "User")
query.whereKey("email", equalTo: username)
query.findObjectsInBackground(onSuccess: { (objects) in
if (objects.count > 0) {
guard let object = objects.first as? PFUser, let username = object.username else{
return
}
PFUser.logInWithUsername(inBackground: username, password: password) { (user, error) in
if let error = error {
onError(error.localizedDescription)
} else {
onSuccess(objects)
}
}
}
else{
PFUser.logInWithUsername(inBackground: username, password: password) { (user, error) in
if let error = error {
onError(error.localizedDescription)
} else {
onSuccess(objects)
}
}
}
}, onError(error.localizedDescription))
}