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.

Fetch the current user's attributes

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

func fetchAttributes() { Amplify.Auth.fetchUserAttributes() { result in switch result { case .success(let attributes): print("User attributes - \(attributes)") case .failure(let error): print("Fetching user attributes failed with error \(error)") } } }
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}

Update user attribute

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

func updateAttribute() { Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444")) { result in do { let updateResult = try result.get() switch updateResult.nextStep { case .confirmAttributeWithCode(let deliveryDetails, let info): print("Confirm the attribute with details send to - \(deliveryDetails) \(info)") case .done: print("Update completed") } } catch { print("Update attribute failed with error \(error)") } } }
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}

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:

func confirmAttribute() { Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") { result in switch result { case .success: print("Attribute verified") case .failure(let error): print("Update attribute failed with error \(error)") } } }
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}

Resend verification code

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

func resendCode() { Amplify.Auth.resendConfirmationCode(for: .email) { result in switch result { case .success(let deliveryDetails): print("Resend code send to - \(deliveryDetails)") case .failure(let error): print("Resend code failed with error \(error)") } } }
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}