【问题标题】:How do a get a specific attribute from AWS Cognito user pools after user?.getDetails call在 user?.getDetails 调用之后如何从 AWS Cognito 用户池中获取特定属性
【发布时间】:2018-08-25 02:21:36
【问题描述】:

成功登录后,我想获取特定属性的值。是否可以在不遍历每个属性的情况下做到这一点?我知道我不能下标,但还有其他方法吗?

所以在下面的示例中,我想要的是用户电子邮件的价值。理想情况下,我想说let email = userAttributes["email"] 但这种类型不允许订阅。

user?.getDetails().continueOnSuccessWith(block: { (task) -> Any? in
    let userAttributes = task.result?.userAttributes

    for attribute in userAttributes! {
        print(attribute.name!)
        print(attribute.value!)
    }
    return nil
})

【问题讨论】:

    标签: ios swift xcode amazon-cognito aws-userpools


    【解决方案1】:

    看到AWSCognitoIdentityProviderAttributeType 包含名称和值,我们可以轻松地映射该数据以创建可以通过键值访问的字典。

    在某处添加这个扩展

    extension Array where Element: AWSCognitoIdentityProviderAttributeType {
        func dict() -> [String : String] {
            var dict: [String : String] = [:]
            self.forEach({ e in dict[e.name!] = e.value! })
            return dict
        }
    }
    

    然后使用

    task.result?.userAttributes.dict()[...]
    

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2017-04-12
      • 2019-01-06
      • 2021-03-09
      • 2022-01-21
      • 1970-01-01
      • 2017-08-25
      • 2019-05-08
      • 2020-04-21
      相关资源
      最近更新 更多