AWSMobileClient

final public class AWSMobileClient : _AWSMobileClient
extension AWSMobileClient: AWSCognitoAuthDelegate
extension AWSMobileClient: AWSIdentityProviderManager

AWSMobileClient is used for all auth related operations when your app is accessing AWS backend.

Public API variables

  • Returns the current state of user. If MobileClient is not initialized, it will return unknown

    Declaration

    Swift

    public var currentUserState: UserState
  • Undocumented

    Declaration

    Swift

    public var deviceOperations: DeviceOperations
  • The singleton instance of AWSMobileClient.

    Deprecation note: use AWSMobileClient.default() since it communicates better the API intent.

    Declaration

    Swift

    @available(*, deprecated, renamed: "default")
    override public class func sharedInstance() -> AWSMobileClient

    Return Value

    The singleton AWSMobileClient instance.

  • The default instance of AWSMobileClient. The configuration is loaded from the awsconfiguration.json file.

    You can manage your own instance of AWSMobileClient by constructing it with AWSMobileClient(configuration), however please note that multiple instances of AWSMobileClient is not supported.

    Implementation Notes:

    The AWSMobileClient relies on AWSInfo for configuration and once that class is initialized, the configuration cannot be reset and/or re-initialized. Therefore, even though you can instantiate AWSMobileClient multiple times, all instances will have the same configuration reference.

    Declaration

    Swift

    @objc
    public class func `default`() -> AWSMobileClient

    Return Value

    The default AWSMobileClient instance

  • Initializes AWSMobileClient and determines the UserState for current user using cache.

    Declaration

    Swift

    public func initialize(_ completionHandler: @escaping (UserState?, Error?) -> Void)

    Parameters

    completionHandler

    Callback which describes current user’s state.

  • Adds a listener who receives notifications on user state change.

    Declaration

    Swift

    public func addUserStateListener(_ object: AnyObject, _ callback: @escaping UserStateChangeCallback)

    Parameters

    object

    The object who intends to receive notification. A strong reference is held to the object and the developer is required to call removeUserStateListener to stop getting notifications and release the object.

    callback

    Callback describing the new user state.

  • Removes a registered listener. If no listener exists, call is ignored.

    Declaration

    Swift

    public func removeUserStateListener(_ object: AnyObject)

    Parameters

    object

    The object to be de-registered from receiving notifications.

  • Updates the service configuration for the Cognito Services

    Warning

    This method is intended for internal use only.

    Declaration

    Swift

    static func updateCognitoService(userPoolConfiguration: AWSServiceConfiguration?,
                                     identityPoolConfiguration: AWSServiceConfiguration?)
  • Returns the username attribute of the access token for the current logged in user, nil otherwise. Note that the value stored may vary depending on how sign-in was performed. @see https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html#amazon-cognito-user-pools-using-the-access-token

    Declaration

    Swift

    public var username: String? { get }
  • Undocumented

    Declaration

    Swift

    public var userSub: String? { get }
  • The identity id associated with this provider. This value will be fetched from the keychain at startup. If you do not want to reuse the existing identity id, you must call the clearKeychain method. If the identityId is not fetched yet, it will return nil. Use getIdentityId() method to force a server fetch when identityId is not available.

    Declaration

    Swift

    override public var identityId: String? { get }
  • Returns true if there is a user currently signed in.

    Declaration

    Swift

    @objc
    public var isSignedIn: Bool { get }
  • Undocumented

    Declaration

    Swift

    public func handleAuthResponse(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any)
  • Fetches the AWSCredentials asynchronously.

    Declaration

    Swift

    public func getAWSCredentials(_ completionHandler: @escaping (AWSCredentials?, Error?) -> Void)

    Parameters

    completionHandler

    completionHandler which would have AWSCredentials if successfully retrieved, else error.

  • Asynchronous deleteUser method which requires network activity.

    Declaration

    Swift

    public func deleteUser(completionHandler: @escaping ((Error?) -> Void))

    Parameters

    completionHandler

    completion handler for success or error callback.

  • When called, will send a confirmation code to user which can be used to reset the password.

    Declaration

    Swift

    public func forgotPassword(username: String,
                               clientMetaData: [String:String] = [:],
                               completionHandler: @escaping ((ForgotPasswordResult?, Error?) -> Void))

    Parameters

    username

    username of the user who forgot the password.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when result is available.

  • Resets the password for the user if they have a valid confirmation code using the specified new password.

    Declaration

    Swift

    public func confirmForgotPassword(username: String,
                                      newPassword: String,
                                      confirmationCode: String,
                                      clientMetaData: [String:String] = [:],
                                      completionHandler: @escaping ((ForgotPasswordResult?, Error?) -> Void))

    Parameters

    username

    username of the user who forgot the password

    newPassword

    the new password which the user wants to set

    confirmationCode

    the confirmation code sent to the user

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when a result is available.

  • Change password of a logged in user.

    Declaration

    Swift

    public func changePassword(currentPassword: String, proposedPassword: String, completionHandler: @escaping ((Error?) -> Void))

    Parameters

    currentPassword

    current password of the user.

    proposedPassword

    the new password which user wants to set.

    completionHandler

    completionHandler which will be called when the result is avilable. If error is nil, the change was successful.

  • Signs in a user with the given username and password.

    Declaration

    Swift

    public func signIn(username: String,
                       password: String,
                       validationData: [String: String]? = nil,
                       clientMetaData: [String: String] = [:],
                       completionHandler: @escaping ((SignInResult?, Error?) -> Void))

    Parameters

    username

    username of the user.

    password

    password of the user.

    validationData

    validation data for this sign in. Overrides any key-value pairs in clientMetadata when the same key exists in validation data.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when result is available.

  • Confirm a sign in which requires additional validation via steps like SMS MFA.

    Declaration

    Swift

    public func confirmSignIn(challengeResponse: String,
                              userAttributes: [String:String] = [:],
                              clientMetaData: [String:String] = [:],
                              completionHandler: @escaping ((SignInResult?, Error?) -> Void))

    Parameters

    challengeResponse

    confirmation code or TOTP token which is available to the user.

    userAttributes

    user attributes required for the operation.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when result is available.

  • Federates a social provider like Google, Facebook, Amazon or Twitter. If user is already signed in through the signIn method, it will return AWSMobileClientError.federationProviderExists error. If federation provider name has changed, previous federation provider’s token will be erased and the new token will be used going forward; the user state is un-affected in that case.

    Declaration

    Swift

    public func federatedSignIn(providerName: String, token: String,
                                federatedSignInOptions: FederatedSignInOptions = FederatedSignInOptions(),
                                completionHandler: @escaping ((UserState?, Error?) -> Void))

    Parameters

    providerName

    Provider name for federation provider, e.g. graph.facebook.com, accounts.google.com

    token

    The idToken

    completionHandler

    completion handler on successful credentials fetch.

  • Shows a fully managed sign in screen which allows users to sign up and sign in.

    Declaration

    Swift

    @available(iOS 13, *)
    public func showSignIn(presentationAnchor: ASPresentationAnchor,
                           hostedUIOptions: HostedUIOptions,
                           _ completionHandler: @escaping(UserState?, Error?) -> Void)

    Parameters

    presentationAnchor

    The presentation Anchor to show the ASWEbAuthenticationSession

    hostedUIOptions

    The options object which allows showSignIn to present a hosted web UI.

    completionHandler

    The completion handler to be called when user finishes the sign in action.

  • Shows a fully managed sign in screen which allows users to sign up and sign in.

    Declaration

    Swift

    public func showSignIn(navigationController: UINavigationController,
                           signInUIOptions: SignInUIOptions = SignInUIOptions(),
                           hostedUIOptions: HostedUIOptions? = nil,
                           _ completionHandler: @escaping(UserState?, Error?) -> Void)

    Parameters

    navigationController

    The navigation controller which would act as the anchor for this UI.

    signInUIOptions

    The options object which allows changing logo, background color and if the user can cancel the sign in operation if using native auth UI. This options object will be ignored if hostedUIOptions is passed in.

    hostedUIOptions

    The options object which allows showSignIn to present a hosted web UI. If passed, signInUIOptions are ignored since they are applicable only to native UI.

    completionHandler

    The completion handler to be called when user finishes the sign in action.

  • Undocumented

    Declaration

    Swift

    public func getViewController() -> UIViewController
  • Undocumented

    Declaration

    Swift

    public func shouldLaunchSignInVCIfRefreshTokenIsExpired() -> Bool
  • Asynchronous signout method which requires network activity. Based on the options specified in SignOutOptions, appropriate tasks will be performed.

    Declaration

    Swift

    @available(iOS 13, *)
    public func signOut(presentationAnchor: ASPresentationAnchor,
                        options: SignOutOptions = SignOutOptions(),
                        completionHandler: @escaping ((Error?) -> Void))

    Parameters

    options

    SignOutOptions which specify actions.

    presentationAnchor

    If you have

    completionHandler

    completion handler for success or error callback.

  • Asynchronous signout method which requires network activity. Based on the options specified in SignOutOptions, appropriate tasks will be performed.

    Declaration

    Swift

    public func signOut(options: SignOutOptions = SignOutOptions(),
                        completionHandler: @escaping ((Error?) -> Void))

    Parameters

    options

    SignOutOptions which specify actions.

    completionHandler

    completion handler for success or error callback.

  • Signs out the current logged in user and clears the local keychain store. Note: This does not invalidate the tokens from the service or sign out the user from other devices.

    Declaration

    Swift

    public func signOut()
  • Invoke this function to release any sign-in waits. When you receive a notifcation which is signedOutFederatedTokensInvalid or signedOutUserPoolsTokensInvalid you need to provide SDK the token via federate method or call the signIn method and complete the sign-in flow. If you can’t get the latest token from the user, you can call this method to un-block any waiting calls.

    Declaration

    Swift

    public func releaseSignInWait()
  • Sign up with username, password and other attrbutes like phone, email.

    Declaration

    Swift

    public func signUp(username: String,
                       password: String,
                       userAttributes: [String: String] = [:],
                       validationData: [String: String] = [:],
                       clientMetaData: [String:String] = [:],
                       completionHandler: @escaping ((SignUpResult?, Error?) -> Void))

    Parameters

    username

    username of the user.

    password

    password of the user

    userAttributes

    user attributes which contain attributes like phone_number, email, etc.

    validationData

    validation data for the user.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when a sign up result is available.

  • Confirms a sign up for a user using a verification code.

    Declaration

    Swift

    public func confirmSignUp(username: String,
                              confirmationCode: String,
                              clientMetaData: [String:String] = [:],
                              completionHandler: @escaping ((SignUpResult?, Error?) -> Void))

    Parameters

    username

    username of the user.

    confirmationCode

    confirmation code sent to the user.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when a result is available.

  • Re-sends the sign up code to the user via email or phone_number.

    Declaration

    Swift

    public func resendSignUpCode(username: String,
                                 clientMetaData: [String:String] = [:],
                                 completionHandler: @escaping ((SignUpResult?, Error?) -> Void))

    Parameters

    username

    username of the user who wants a new registration code.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when a result is available.

  • Returns cached UserPools auth JWT tokens if valid. If the idToken is not valid, and a refresh token is available, refresh token is used to get a new idToken. If there is no refresh token and the user is signed in, a notification is dispatched to indicate requirement of user to re-signin. The call to wait will be synchronized so that if multiple threads call this method, they will block till the first thread gets the token.

    Declaration

    Swift

    public func getTokens(_ completionHandler: @escaping TokenCompletion)

    Parameters

    completionHandler

    Tokens if available, else error.

  • Verify a user attribute like phone_number.

    This method is only valid for users signed in via UserPools (either directly or via HostedUI).

    Declaration

    Swift

    public func verifyUserAttribute(attributeName: String,
                                    clientMetaData: [String:String] = [:],
                                    completionHandler: @escaping ((UserCodeDeliveryDetails?, Error?) -> Void))

    Parameters

    attributeName

    name of the attribute.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when the result is avilable.

  • Update the attributes for a user.

    This method is only valid for users signed in via UserPools (either directly or via HostedUI).

    Declaration

    Swift

    public func updateUserAttributes(attributeMap: [String: String],
                                     clientMetaData: [String:String] = [:],
                                     completionHandler: @escaping (([UserCodeDeliveryDetails]?, Error?) -> Void))

    Parameters

    attributeMap

    the attribute map of the user.

    clientMetaData

    A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.

    completionHandler

    completionHandler which will be called when the result is avilable.

  • Fetches the attributes for logged in user.

    This method is only valid for users signed in via UserPools (either directly or via HostedUI).

    Declaration

    Swift

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

    Parameters

    completionHandler

    completion handler which will be invoked when result is available.

  • Confirm the updated attributes using a confirmation code.

    This method is only valid for users signed in via UserPools (either directly or via HostedUI).

    Declaration

    Swift

    public func confirmUpdateUserAttributes(attributeName: String, code: String, completionHandler: @escaping ((Error?) -> Void))

    Parameters

    attributeName

    the attribute to be confirmed.

    code

    the code sent to the user.

    completionHandler

    completionHandler which will be called when the result is avilable.

  • Confirm the attribute using a confirmation code.

    This method is only valid for users signed in via UserPools (either directly or via HostedUI).

    Declaration

    Swift

    public func confirmVerifyUserAttribute(attributeName: String, code: String, completionHandler: @escaping ((Error?) -> Void))

    Parameters

    attributeName

    the attribute to be verified.

    code

    the code sent to the user.

    completionHandler

    completionHandler which will be called when the result is avilable.

  • Asynchronously returns a valid AWS credentials or an error object if it cannot retrieve valid credentials. It should cache valid credentials as much as possible and refresh them when they are invalid.

    Declaration

    Swift

    override public func credentials() -> AWSTask<AWSCredentials>

    Return Value

    A valid AWS credentials or an error object describing the error.

  • Invalidates the cached temporary AWS credentials. If the credentials provider does not cache temporary credentials, this operation is a no-op.

    Declaration

    Swift

    override public func invalidateCachedTemporaryCredentials()
  • Get/retrieve the identity id for this provider. If an identity id is already set on this provider, no remote call is made and the identity will be returned as a result of the AWSTask (the identityId is also available as a property). If no identityId is set on this provider, one will be retrieved from the service.

    Declaration

    Swift

    override public func getIdentityId() -> AWSTask<NSString>

    Return Value

    Asynchronous task which contains the identity id or error.

  • Clear the cached AWS credentials for this provider.

    Declaration

    Swift

    override public func clearCredentials()
  • Clear ALL saved values for this provider (identityId, credentials, logins).

    Declaration

    Swift

    override public func clearKeychain()

AWSIdentityProviderManager protocol methods

  • Each entry in logins represents a single login with an identity provider. The key is the domain of the login provider (e.g. ‘graph.facebook.com’) and the value is the OAuth/OpenId Connect token that results from an authentication with that login provider.

    Declaration

    Swift

    public func logins() -> AWSTask<NSDictionary>