Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Feb 21, 2024

Manage user attributes

Amplify iOS v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Swift to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

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.

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)")
}
}
}
func fetchAttributes() -> AnyCancellable {
Amplify.Auth.fetchUserAttributes()
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Fetch user attributes failed with error \(authError)")
}
}
receiveValue: { attributes in
print("User attributes - \(attributes)")
}
}

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)")
}
}
}
func updateAttribute() -> AnyCancellable {
Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444"))
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Update attribute failed with error \(authError)")
}
}
receiveValue: { updateResult in
switch updateResult.nextStep {
case .confirmAttributeWithCode(let deliveryDetails, let info):
print("Confirm the attribute with details send to - \(deliveryDetails) \(info)")
case .done:
print("Update completed")
}
}
}

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)")
}
}
}
func confirmAttribute() -> AnyCancellable {
Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739")
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Update attribute failed with error \(authError)")
}
}
receiveValue: { _ in
print("Attribute verified")
}
}

Send user attribute verification code

If an attribute needs to be verified while the user is authenticated, invoke the 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)")
}
}
}
func resendCode() -> AnyCancellable {
Amplify.Auth.resendConfirmationCode(for: .email)
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Resend code failed with error \(authError)")
}
}
receiveValue: { deliveryDetails in
print("Resend code sent to - \(deliveryDetails)")
}
}