我开始找到使用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(当有人登录时)。 我认为必须有一种方法。我该怎么办?
答案 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)
我相信您要查找的userId是AWSMobileClient.default().getIdentityId()