AWS Cognito User Pools with Mobile SDK for iOS Using Custom Challenge


Welcome back, everyone, to another episode of Continuous Improvement, the podcast where we explore various challenges and solutions encountered in the world of technology. I’m your host, Victor, and today we’ll be diving into the topic of integrating an AWS Cognito User Pool into an iOS application.

A couple of weeks ago, I faced this very challenge while working on an iOS app. The sign-in feature required a custom challenge for authentication, but I found that there was limited documentation available on how to use the iOS SDK for this purpose. After a series of trials and errors, I finally managed to crack the code and get it working. Today, I’ll be sharing the steps I took to accomplish this, hoping to make it easier for others facing a similar situation.

[SFX TRANSITION]

Step one involves creating a Cognito User Pool. In the AppDelegate, after didFinishLaunchingWithOptions, I initialized the user pool as follows:

[CODE EXAMPLE]

Here, we set up the service configuration and create the pool configuration using the appropriate values. Initializing the user pool client is crucial, and we fetch the initialized user pool client for later use. Additionally, don’t forget to set the delegate of the user pool to self.

[SFX TRANSITION]

Step two involves implementing the protocol delegate. This is done through an extension of the AppDelegate class that conforms to the AWSCognitoIdentityCustomAuthentication protocol.

[CODE EXAMPLE]

Inside this extension, we implement three functions: didCompleteStepWithError, getCustomChallengeDetails, and startCustomAuthentication. These functions play a crucial role in handling the custom authentication flow. The startCustomAuthentication function ensures that the sign-in view controller is presented to the user when custom authentication is required.

[SFX TRANSITION]

Step three takes us inside the sign-in view controller. We need to handle the custom challenge within this view controller, and we do so by extending the SignInViewController class to conform to AWSCognitoIdentityCustomAuthentication.

[CODE EXAMPLE]

As you can see, we implement the getCustomChallengeDetails function, where we provide the necessary challenge responses. This is where the user’s username and answer are set.

[SFX TRANSITION]

Lastly, step four allows us to access the user attributes after a successful sign-in. After the user signs in, we can use the following code snippet to retrieve and display their details.

[CODE EXAMPLE]

By calling getDetails on the user object, we can fetch the user’s details and display them accordingly.

[SFX TRANSITION]

And there you have it—a simplified walkthrough of integrating an AWS Cognito User Pool with custom authentication into an iOS application. While the lack of documentation can be frustrating, I hope the steps I’ve provided will help save you some time and effort.

[SFX TRANSITION]

As always, if you have any questions or need further clarification on any of the steps, please feel free to reach out to me. We can chat about it on Twitter using the handle @ContinuousImp, or you can visit our website, ContinuousImprovementPodcast.com, to access additional resources.

That’s it for today’s episode of Continuous Improvement. Thank you for tuning in, and remember, no matter how challenging the journey, the key is to keep improving. Until next time!