Swift 使用 AWSCognitoIdentityProvider 获取手机号码和绑定手机号码
在使用 AWSCognitoIdentityProvider 进行手机号码认证时,你可以使用以下方法来获取手机号码和绑定手机号码:
- 首先,你需要创建一个 AWSCognitoIdentityProvider 对象。例如,你可以使用以下代码创建一个对象:
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
poolId: 'YOUR_POOL_ID')
let pool = AWSCognitoIdentityUserPool(forKey: 'userPool')
AWSCognitoIdentityProvider.register(with: userPoolConfiguration, forKey: 'userPool')
let userPool = AWSCognitoIdentityProvider(forKey: 'userPool')
- 现在,你可以使用
userPool对象来获取用户的手机号码和绑定手机号码。例如,以下代码将获取当前已登录用户的手机号码:
if let currentUser = pool.currentUser() {
currentUser.getDetails().continueWith { (task) -> Any? in
if let error = task.error {
print('Error: (error)')
} else if let result = task.result {
if let phoneAttribute = result.userAttributes?.first(where: { $0.name == 'phone_number' }) {
let phoneNumber = phoneAttribute.value
print('Phone number: (phoneNumber)')
}
}
return nil
}
}
- 如果你想获取用户的绑定手机号码,你可以使用以下代码:
if let currentUser = pool.currentUser() {
currentUser.listDevices().continueWith { (task) -> Any? in
if let error = task.error {
print('Error: (error)')
} else if let result = task.result {
if let device = result.devices?.first {
let phoneNumber = device.deviceAttributes?.first(where: { $0.name == 'phone_number' })?.value
print('Phone number: (phoneNumber)')
}
}
return nil
}
}
请注意,这些代码假设你已经成功进行了用户登录,并且当前已登录的用户是有效的。
原文地址: https://www.cveoy.top/t/topic/peq8 著作权归作者所有。请勿转载和采集!