gpt4 book ai didi

ios - 在 Swift-iOS 中,我如何在 AWS Cognito 中捕获用户身份验证错误?

转载 作者:搜寻专家 更新时间:2023-11-01 06:36:06 24 4
gpt4 key购买 nike

我正在尝试 aws cognito 用户池并在用户注册过程中堆积。我已经配置了我的用户池并正在执行注册方法,但我可以找到一种方法来获取 aws 服务返回的错误代码。这是我的用户池实例,工作正常:

let poolConfig = AWSCognitoIdentityUserPoolConfiguration(
clientId: userPool_clientId,
clientSecret: userPool_secret,
poolId: userPool_id)

AWSCognitoIdentityUserPool.registerCognitoIdentityUserPool(with: poolConfig,
forKey: userPoll_App)

userPool = AWSCognitoIdentityUserPool(forKey: userPoll_App)

然后,在我的 View Controller 中,我有一个带有 @IBAction 的按钮:

    if userPool != nil {

let attName = AWSCognitoIdentityUserAttributeType()!
attName.name = "name"
attName.value = userNome

let attLast = AWSCognitoIdentityUserAttributeType()!
attLast.name = "family name"
attLast.value = userSobrenome

let attEmail = AWSCognitoIdentityUserAttributeType()!
attEmail.name = "email"
attEmail.value = userEmail

var result:Bool = false
userPool!.signUp(userNome,
password: userPwd,
userAttributes: [attName, attLast, attEmail],
validationData: nil).continue({(task:AWSTask!) in

if (task.error != nil) {
print (task.error!)
result = false
} else {
result = true
}

return nil

})

在该代码之后,我测试结果以查看它是真还是假,然后采取适当的措施。但是……

我在这个过程中遇到了不同的错误,我需要在开发时评估这些错误。例如,我遇到的第一个错误是因为我错误地配置了 AWS 区域。行!游戏开始!!但是第二个错误是因为用户告知的密码没有通过池的验证。在这种情况下,我想知道错误是因为验证过程并通知用户采取适当的操作。我不想在 iOS 应用程序中有这种逻辑。 task.error 对象只是提供了一个本地化的描述属性,它不是很有用。

顺便说一句:我使用的是 Swift 3.2、iOS 10.2、aws-ios-sdk2 和 Xcode 8。

最佳答案

我想扩展 behrooziAWS 的回答。

在 Swift 4 中,您可以将错误代码与 AWSCognitoIdentityProviderErrorType.TheErrorType.rawValue 等枚举相匹配。

这里有一个搜索错误类型的提示,只需键入“AWSErrorType”,Xcode 的自动完成功能就会显示所有枚举,然后您就可以浏览它们了。

这是我使用的代码。

AWSobject.AWSfunction().continueWith { task -> Any? in
if let err = task.error as NSError? {
switch err.code {
case: AWSCognitoIdentityProviderErrorType.userNotFound.rawValue:
// Handle case
default:
// Handle all other cases here
return nil
}

// Do stuff on success!
}

关于ios - 在 Swift-iOS 中,我如何在 AWS Cognito 中捕获用户身份验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41471039/

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