Page updated Nov 14, 2023

Manage user attributes

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Fetch the current user's attributes

Invoke the following api to get the list of attributes assigned to the user.

1func fetchAttributes() {
2 Amplify.Auth.fetchUserAttributes() { result in
3 switch result {
4 case .success(let attributes):
5 print("User attributes - \(attributes)")
6 case .failure(let error):
7 print("Fetching user attributes failed with error \(error)")
8 }
9 }
10}
1func fetchAttributes() -> AnyCancellable {
2 Amplify.Auth.fetchUserAttributes()
3 .resultPublisher
4 .sink {
5 if case let .failure(authError) = $0 {
6 print("Fetch user attributes failed with error \(authError)")
7 }
8 }
9 receiveValue: { attributes in
10 print("User attributes - \(attributes)")
11 }
12}

Update user attribute

Invoke the update api for creating new or updating existing user attributes.

1func updateAttribute() {
2 Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444")) { result in
3 do {
4 let updateResult = try result.get()
5 switch updateResult.nextStep {
6 case .confirmAttributeWithCode(let deliveryDetails, let info):
7 print("Confirm the attribute with details send to - \(deliveryDetails) \(info)")
8 case .done:
9 print("Update completed")
10 }
11 } catch {
12 print("Update attribute failed with error \(error)")
13 }
14 }
15}
1func updateAttribute() -> AnyCancellable {
2 Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444"))
3 .resultPublisher
4 .sink {
5 if case let .failure(authError) = $0 {
6 print("Update attribute failed with error \(authError)")
7 }
8 }
9 receiveValue: { updateResult in
10 switch updateResult.nextStep {
11 case .confirmAttributeWithCode(let deliveryDetails, let info):
12 print("Confirm the attribute with details send to - \(deliveryDetails) \(info)")
13 case .done:
14 print("Update completed")
15 }
16 }
17}

Verify user attribute

Some attributes require confirmation for the attribute update to complete. If the attribute need to be confirmed, the result of the above api will be CONFIRM_ATTRIBUTE_WITH_CODE. A confirmation code will be sent to the delivery medium mentioned in the delivery details. When the user gets the confirmation code, you can present a UI to the user to enter the code and invoke the confirm attribute api with their input:

1func confirmAttribute() {
2 Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") { result in
3 switch result {
4 case .success:
5 print("Attribute verified")
6 case .failure(let error):
7 print("Update attribute failed with error \(error)")
8 }
9 }
10}
1func confirmAttribute() -> AnyCancellable {
2 Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739")
3 .resultPublisher
4 .sink {
5 if case let .failure(authError) = $0 {
6 print("Update attribute failed with error \(authError)")
7 }
8 }
9 receiveValue: { _ in
10 print("Attribute verified")
11 }
12}

Resend verification code

If the code has expired or the user needs to resend the confirmation code, invoke the resend api as shown below:

1func resendCode() {
2 Amplify.Auth.resendConfirmationCode(for: .email) { result in
3 switch result {
4 case .success(let deliveryDetails):
5 print("Resend code send to - \(deliveryDetails)")
6 case .failure(let error):
7 print("Resend code failed with error \(error)")
8 }
9 }
10}
1func resendCode() -> AnyCancellable {
2 Amplify.Auth.resendConfirmationCode(for: .email)
3 .resultPublisher
4 .sink {
5 if case let .failure(authError) = $0 {
6 print("Resend code failed with error \(authError)")
7 }
8 }
9 receiveValue: { deliveryDetails in
10 print("Resend code sent to - \(deliveryDetails)")
11 }
12}