YoanGJ
YoanGJ

Reputation: 509

Get user attributes AWS Amplify iOS SDK

I'm building an iOS app with the SDK Amplify so I have my users registered on AWS.

I already have my sign in/sign up flow working but the problem is that with the newest version of the SDK I have absolutely no idea of how can I get attributes of a registered user like his family name, email address etc...

With this new SDK everything seems to work around the AWSMobileClientclass but I see nothing from this class that can help me to get what I want.

The official documentation is anemic and doesn't cover or even point to my use case.

If somebody can give me some hint or even some good ressources I'll be very thankful!

Upvotes: 2

Views: 2520

Answers (3)

Rohan Dubal
Rohan Dubal

Reputation: 847

The method was missing from the initial release and has since been added. You can use the getUserAttributes with the following API in the latest SDK version 2.8.x:

public func getUserAttributes(completionHandler: @escaping (([String: String]?, Error?) -> Void))

You can find the source code here:

https://github.com/aws-amplify/aws-sdk-ios/blob/master/AWSAuthSDK/Sources/AWSMobileClient/AWSMobileClientExtensions.swift#L532

Upvotes: 2

Karthick Selvaraj
Karthick Selvaraj

Reputation: 2505

For a note:

Make sure you did configure attribute read and write permission accordingly in your Cognito user pool App client to access your user attributes using getUserAttributes.

To configure attributes read and write permissions in user pool,

User pool -> General settings -> App clients -> Choose your app client -> Show details -> Set attribute read and write permissions

enter image description here

Thanks!

Upvotes: 0

donkon
donkon

Reputation: 919

Hi YoanGJ and future guests,

Based on your comment you were looking for some sample code.

    AWSMobileClient.sharedInstance().getUserAttributes { (attributes, error) in
            if let attributes = attributes {
                XCTAssertTrue(attributes.count == 3, "Expected 3 attributes for user.")
                XCTAssertTrue(attributes["email_verified"] == "false", "Email should not be verified.")
            }else if let error = error {
                XCTFail("Received un-expected error: \(error.localizedDescription)")
            }
            getAttrExpectation.fulfill()
}

This excerpt shows how you could call getUserAttributes and it comes from the integration tests found here.

Upvotes: 3

Related Questions