gpt4 book ai didi

swift - AWS 认知 : Trying to Alert User of Error

转载 作者:行者123 更新时间:2023-11-28 06:25:10 27 4
gpt4 key购买 nike

我在捕获 Cognito 注册错误时遇到困难。当 Cognito 返回“UsernameExistsException”、“message”:“User already exists”错误时,我试图提醒用户。下面是我的代码:

self.pool!.signUp(usernameTextField.text!, password: passwordTextField.text!, userAttributes: attributes,   validationData: nil).continue(successBlock: { (task:AWSTask!) in
// needs to be async so we can ALWAYS return nil for AWSTask
DispatchQueue.main.async {
if task.error != nil { // some sort of error
let myerror = task.error
print("\(myerror)")
let alert = UIAlertController(title: "Sign Up Error", message: (task.error?.localizedDescription)! as String, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else {
let response: AWSCognitoIdentityUserPoolSignUpResponse = task.result! as AWSCognitoIdentityUserPoolSignUpResponse
// NSLog("AWSCognitoIdentityUserPoolSignUpResponse: \(response)")
self.user = response.user
let alert = UIAlertController(title: "Sign Up Successful", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in self.performSegue(withIdentifier: "confrimationSegue", sender: self) }))
self.present(alert, animated: true, completion: nil)

}
}
return nil
})

由于某种原因,我无法进入任务。错误!= nil 条件语句。当我强制错误时,错误不会打印出来,并且警报操作不会出现在 View 中。我是否试图在错误的功能中提醒用户?我还能如何检查 cognito 出现的用户名已存在错误。提前致谢。

最佳答案

您正在使用 successBlock,这意味着您设置的 block 只会在注册成功执行时被调用。这就是错误始终为 nil 的原因。

为了避免错误,您应该简单地通过如下方式设置回调:

userPool
.signUp(user.email, password: user.password, userAttributes: attributes, validationData: nil)
.continue({ response in
if let error = response.error {
// Error ocurred
} else {
// No error ocurred
})

continue 方法只接收回调,并在发生错误时调用。

关于swift - AWS 认知 : Trying to Alert User of Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145512/

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