Working with the API

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

You can view the Mobile SDK API reference here.

SignUp

Creates a new user in Cognito User Pools:

1AWSMobileClient.default().signUp(
2 username: "your_username",
3 password: "Abc@123!",
4 userAttributes: ["email":"john@doe.com", "phone_number": "+1973123456"]
5) { (signUpResult, error) in
6 if let signUpResult = signUpResult {
7 switch(signUpResult.signUpConfirmationState) {
8 case .confirmed:
9 print("User is signed up and confirmed.")
10 case .unconfirmed:
11 print("User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
12 case .unknown:
13 print("Unexpected case")
14 }
15 } else if let error = error {
16 if let error = error as? AWSMobileClientError {
17 switch(error) {
18 case .usernameExists(let message):
19 print(message)
20 default:
21 break
22 }
23 }
24 print("\(error.localizedDescription)")
25 }
26}

User Attributes

You can provide custom user attributes in the signUp() method by passing them into the userAttributes argument as key-value pairs. For example:

1AWSMobileClient.default().signUp(
2 username: "your_username",
3 password: "Abc@123!",
4 userAttributes: [
5 "nickname":"Johnny",
6 "badge_number": "ABC123XYZ"
7]) { (signUpResult, error) in
8 //Use results as before
9}

Confirm SignUp

Confirms a new user after signing up (MFA):

1AWSMobileClient.default().confirmSignUp(
2 username: "your_username",
3 confirmationCode: signUpCodeTextField.text!
4) { (signUpResult, error) in
5 if let signUpResult = signUpResult {
6 switch(signUpResult.signUpConfirmationState) {
7 case .confirmed:
8 print("User is signed up and confirmed.")
9 case .unconfirmed:
10 print("User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
11 case .unknown:
12 print("Unexpected case")
13 }
14 } else if let error = error {
15 print("\(error.localizedDescription)")
16 }
17}

Re-send a Confirmation Code (MFA)

1AWSMobileClient.default().resendSignUpCode(
2 username: "your_username",
3 completionHandler: { (result, error) in
4 if let signUpResult = result {
5 print("A verification code has been sent via \(signUpResult.codeDeliveryDetails!.deliveryMedium) at \(signUpResult.codeDeliveryDetails!.destination!)")
6 } else if let error = error {
7 print("\(error.localizedDescription)")
8 }
9 }
10)

SignIn

Sign in with user credentials:

1AWSMobileClient.default().signIn(
2 username: "your_username",
3 password: "Abc@123!"
4) { (signInResult, error) in
5 if let error = error {
6 print("\(error.localizedDescription)")
7 } else if let signInResult = signInResult {
8 switch (signInResult.signInState) {
9 case .signedIn:
10 print("User is signed in.")
11 case .smsMFA:
12 print("SMS message sent to \(signInResult.codeDetails!.destination!)")
13 default:
14 print("Sign In needs info which is not et supported.")
15 }
16 }
17}

Confirm SignIn (MFA)

1AWSMobileClient.default().confirmSignIn(
2 challengeResponse: "code_here"
3) { (signInResult, error) in
4 if let error = error {
5 print("\(error.localizedDescription)")
6 } else if let signInResult = signInResult {
7 switch (signInResult.signInState) {
8 case .signedIn:
9 print("User is signed in.")
10 default:
11 print("\(signInResult.signInState.rawValue)")
12 }
13 }
14}

Force a Password Reset

If a user is required to change their password on first login, a newPasswordRequired state will be returned when signIn is called and you will need to provide a new password. This can be done by using confirmSignIn:

1AWSMobileClient.default().signIn(
2 username: "abc123",
3 password: "Abc123!@"
4) { (signInResult, error) in
5 if let signInResult = signInResult {
6 switch(signInResult.signInState) {
7 case .signedIn:
8 print("User signed in successfully.")
9 case .smsMFA:
10 print("Code was sent via SMS to \(signInResult.codeDetails!.destination!)")
11 case .newPasswordRequired:
12 print("A change of password is needed. Please provide a new password.")
13 default:
14 print("Other signIn state: \(signInResult.signInState)")
15 }
16 } else if let error = error {
17 print("Error occurred: \(error.localizedDescription)")
18 }
19}
20
21AWSMobileClient.default().confirmSignIn(
22 challengeResponse: "NEW_PASSWORD_HERE"
23) { (signInResult, error) in
24 if let signInResult = signInResult {
25 switch(signInResult.signInState) {
26 case .signedIn:
27 print("User signed in successfully.")
28 case .smsMFA:
29 print("Code was sent via SMS to \(signInResult.codeDetails!.destination!)")
30 default:
31 print("Other signIn state: \(signInResult.signInState)")
32 }
33 } else if let error = error {
34 print("Error occurred: \(error.localizedDescription)")
35 }
36}

SignOut

1AWSMobileClient.default().signOut()

Global SignOut

Using global signout, you can signout a user from all active login sessions. By doing this, you are invalidating all tokens (id token, access token and refresh token) which means the user is signed out from all devices.

Note Although the tokens are revoked the temporary AWS credentials (Access and Secret Keys) will remain valid until they expire, which by default is 1 hour.

1AWSMobileClient.default().signOut(
2 options: SignOutOptions(signOutGlobally: true)
3) { (error) in
4 print("Error: \(error.debugDescription)")
5}

Forgot Password

Forgot password is a 2 step process.

  1. Call a forgotPassword() method which sends a confirmation code via email or phone number. The details of how the code was sent are included in the response of forgotPassword().
  2. Once the code is given call confirmForgotPassword() with the confirmation code.
1AWSMobileClient.default().forgotPassword(
2 username: "my_username"
3) { (forgotPasswordResult, error) in
4 if let forgotPasswordResult = forgotPasswordResult {
5 switch(forgotPasswordResult.forgotPasswordState) {
6 case .confirmationCodeSent:
7 print("Confirmation code sent via \(forgotPasswordResult.codeDeliveryDetails!.deliveryMedium) to: \(forgotPasswordResult.codeDeliveryDetails!.destination!)")
8 default:
9 print("Error: Invalid case.")
10 }
11 } else if let error = error {
12 print("Error occurred: \(error.localizedDescription)")
13 }
14}
1AWSMobileClient.default().confirmForgotPassword(
2 username: "my_username",
3 newPassword: "MyNewPassword123!!",
4 confirmationCode: "ConfirmationCode"
5) { (forgotPasswordResult, error) in
6 if let forgotPasswordResult = forgotPasswordResult {
7 switch(forgotPasswordResult.forgotPasswordState) {
8 case .done:
9 print("Password changed successfully")
10 default:
11 print("Error: Could not change password.")
12 }
13 } else if let error = error {
14 print("Error occurred: \(error.localizedDescription)")
15 }
16}

Utility Properties

AWSMobileClient provides several property helpers that are automatically cached locally.

1AWSMobileClient.default().username //String
2AWSMobileClient.default().isSignedIn //Boolean
3AWSMobileClient.default().identityId //String

Note: The property username is available only when using username-password based auth with Cognito User Pools.

Managing Security Tokens

When using Authentication with AWSMobileClient, you do not need to refresh Amazon Cognito tokens manually. The tokens are automatically refreshed by the SDK when necessary.

OIDC Tokens

1AWSMobileClient.default().getTokens { (tokens, error) in
2 if let error = error {
3 print("Error getting token \(error.localizedDescription)")
4 } else if let tokens = tokens {
5 print(tokens.accessToken!.tokenString!)
6 }
7}

AWS Credentials

1AWSMobileClient.default().getAWSCredentials { (credentials, error) in
2 if let error = error {
3 print("\(error.localizedDescription)")
4 } else if let credentials = credentials {
5 print(credentials.accessKey)
6 }
7}