Search code examples
iosswiftamazon-cognitoaws-mobilehub

How to retrieve email and phone_number from AWS Mobile Hub in Swift?


Does anyone know how to retrieve email and phone_number (just as the following picture shows) from AWS Mobile Hub in Swift?

Picture

I only know how to retrieve an username with the following code:

  let serviceConfiguration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:nil)
  let configuration = AWSCognitoIdentityUserPoolConfiguration(clientId: "clientId", clientSecret: "clientSecret", poolId: "poodId")
  AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: configuration, forKey: "Users")
  let pool = AWSCognitoIdentityUserPool(forKey: "Users")
  print(pool.currentUser()?.username)

Any suggestions would be appreciated.


Solution

  • Here is the correct way:

    @objc func retrieveFromAttributes() {
    
            let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
            let newProfileAttributes: ProfileAttributes = ProfileAttributes()
            newProfileAttributes._userId = AWSIdentityManager.default().identityId
            dynamoDbObjectMapper.load(ProfileAttributes.self, hashKey: newProfileAttributes._userId, rangeKey: "Vincent Sun", completionHandler: { (objectModel: AWSDynamoDBObjectModel?, error: Error?) -> Void in
                if let error = error {
                    print("Amazon DynamoDB Read Error: \(error)")
                    return
                }
                    print("An item was read.")
    
                    DispatchQueue.main.async {
                    self.nameTextField.text = objectModel?.dictionaryValue["_nickname"] as? String
                }
    
    
            })
    
    
        }