使用AWS / iOS查找用户ID

时间:2019-05-15 09:48:23

标签: ios swift amazon-web-services authentication

我开始找到使用AWS的方法。 但是我有一个简单的问题,我没有尝试通过搜寻网络来寻找答案,而是没有找到答案。这是相关的工作代码:

override func viewDidLoad() {
    super.viewDidLoad()
    ...

    if AWSSignInManager.sharedInstance().isLoggedIn {
        print("We are already logged in!")
        // I would like to display the user identification of the current user.
    }

    ...
}

在注释的位置,我想打印当前用户的用户ID(当有人登录时)。 我认为必须有一种方法。我该怎么办?

2 个答案:

答案 0 :(得分:1)

也许考虑Cognito“用户名”字段,它是每个用户的唯一ID(不可变):

        let amc = AWSMobileClient.sharedInstance();
        if (amc.isSignedIn) {
             print ("Signed in with username=\(amc.username!)")
        }

我们还可以使用getUserAttributes来获取所有属性,如下所示:

            // See all the attributes the user has
            amc.getUserAttributes { (attributes, error) in
                if let attributes = attributes {
                    for (key, value) in attributes {
                        print("\(key): \(value)")
                    }
                } else if let error = error {
                    print("Unexpected error: \(error.localizedDescription)")
                }
            }

答案 1 :(得分:0)