gpt4 book ai didi

swift - 如何使用 PFQuery 检查用户信息

转载 作者:行者123 更新时间:2023-11-30 12:59:17 26 4
gpt4 key购买 nike

当用户注册时,他/她会被重定向到另一个 View Controller ,他们需要在其中验证他们的电话号码。我尝试设置 PFQuery 从 Parse 检索用户的代码,并查看它是否与验证文本字段中写入的内容匹配,但是,它总是导致用户被重定向到主视图 Controller ,无论输入的代码是否正确或错误。我也尝试过使用 objectForKey (currentUser) 和查询 if phoneCode != currentUser 来执行此操作,但结果是相同的。我想做的是检查输入的代码是否正确,并根据响应将用户重定向到另一个 View Controller 。

@IBAction func submitCodeTapped(sender: AnyObject) {
let currentUser = PFUser.currentUser()?.objectForKey("phoneVerification") as? NSValue
let code = codeTextField.text
let query = PFQuery(className: "User")
let phoneCode = query.whereKey("phoneVerification", equalTo: code!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error != nil || objects != nil {
//if phoneCode != currentUser{
self.displayAlert2("Wrong Code", message: "This is not the code you were sent.")
}else{
let myUser:PFUser = PFUser.currentUser()!
myUser.setObject(true, forKey: "phoneVerified")
myUser.saveInBackgroundWithBlock { (success, error) -> Void in
if error == nil{
print("Successfully set the object.")
self.displayAlert("Great!", message: "Your phone number has been verified!", error: nil)
let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.buildUserInterface()
}else{
//let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("signInPage")
//self.navigationController?.pushViewController(loginVC!, animated: true)
print("Erreur")

}
}
}
})
}

最佳答案

在第一个 if 语句中,您告诉服务器如果查找对象时出错或查询找到了某些对象,则显示警报。但是假设用户输入了错误的代码,查询将返回一个空数组(注意它不会返回 nil,它是一个空数组),但不会有任何错误,因此执行 else block 。

由于您的“phoneVerification”是一个数字,我们需要将 codeTextField.text 转换为 NSNumber:

let code = NSNumber(value:Int(codeTextField.text)!)
let query = PFQuery(className: "_User")
query.whereKey("phoneVerification", equalTo: code!)

您可以将第一个 if 语句更改为:

if ((error != nil && error! as! Bool) || objects?.count == 0) {
//handle the error here.
}

关于swift - 如何使用 PFQuery 检查用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40034671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com