AWS Mobile
Developer Guide

Add Authentication to the Notes App

In the previous section of this tutorial, we created a mobile backend project in AWS Mobile Hub, then added analytics to the sample note-taking app. This section assumes you have completed those steps. If you jumped to this step, please go back and start from the beginning. In this tutorial, we will configure a sign-up / sign-in flow in our mobile backend. We will then add a new authentication activity to our note-taking app.

You should be able to complete this section of the tutorial in 20-30 minutes.

Setup Your Backend

To add User Sign-in to your app you will create the backend resources in your Mobile Hub project, and then update the configuration file in your app.

Add User Sign-in to the AWS Mobile Hub Project

  1. Right-click awsconfiguration.json in your Xcode Project Navigator, choose Delete, and then choose Move to trash.

  2. Open the AWS Mobile Hub console.

  3. Select your project.

  4. Scroll down to the Add More Backend Features section.

  5. Choose the User Sign-in tile.

  6. Choose Email and Password.

  7. Select the Username radio button and the Phone checkbox under it.

  8. Select Required for Multi-factor authentication.

  9. At the bottom of the page, set the Require user sign-in? switch to YES.

    What does this do?

    You have just created your own user pool in the Amazon Cognito service. When used in conjunction with the AWS Mobile sign-in process, the user pool enforces the password requirement rules you chose. It also supports sign-up and forgot my password user flows.

  10. Choose your project name in the upper left and then choose Integrate on your iOS app card.

  11. Choose Download Cloud Config to get an awsconfiguration.json file updated with the new services.


    Whenever you update the AWS Mobile Hub project, a new AWS configuration file for your app is generated.

Connect to Your Backend

To update the linkage between your app and your AWS services:

  1. Drag awsconfiguration.json from your download location into the Xcode project folder containing Info.plist. Select Copy items if needed and Create groups in the options dialog. Choose Finish.


Your system may have modified the filename to avoid conflicts. Make sure the file you add to your Xcode project is named awsconfiguration.json.

Add Auth Dependencies

  1. Add the following Auth dependencies in your project's Podfile

    platform :ios, '9.0' target :'MyNotes' do use_frameworks! # Analytics dependency pod 'AWSPinpoint', '~> 2.6.5' # Auth dependencies pod 'AWSUserPoolsSignIn', '~> 2.6.5' pod 'AWSAuthUI', '~> 2.6.5' pod 'AWSMobileClient', '~> 2.6.5' # other pods end

    Then, in a terminal run:

    pod install --repo-update

    If you encounter an error message that begins "[!] Failed to connect to GitHub to update the CocoaPods/Specs . . .", and your internet connectivity is working, you may need to update openssl and Ruby.

Create an AWSMobileClient and Initialize the SDK

Import AWSMobileClient and add the following function into the AppDelegate class of AppDelegate.swift. This will create an instance of AWSMobileClient.

import UIKit import CoreData // Anaytics imports import AWSCore import AWSPinpoint // Auth imports import AWSMobileClient @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { // . . . //Instantiate the AWSMobileClient func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return AWSMobileClient.sharedInstance().interceptApplication( application, open: url, sourceApplication: sourceApplication, annotation: annotation) } // . . . }

In didFinishLaunching call the AWSMobileClient to register your user pool as the identity provider that enables users to access your app's AWS resources.

func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Initialize AWSMobileClient let didFinishLaunching = AWSMobileClient.sharedInstance().interceptApplication( application, didFinishLaunchingWithOptions: launchOptions) // Initialize Pinpoint to enable session analytics let pinpoint = AWSPinpoint(configuration: AWSPinpointConfiguration.defaultPinpointConfiguration( launchOptions: launchOptions)) return didFinishLaunching }

What did this do?

This will register your sign in providers and fetch the user pool you created and fetch an identity that enables a user to access your app's AWS resources. In this case, the provider is an Amazon Cognito user pool, but federating Facebook, Google, SAML and other identity providers is also supported.

Implement Your Sign-in UI

The AWS Mobile SDK provides a library that creates a customizable sign-in UI in your app. To create your sign-in UI, add the following imports and then call the library in the viewDidLoad() function of MasterViewController.swift .

import AWSCore import AWSPinpoint import UIKit import AWSAuthCore import AWSAuthUI class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate { // . . . override func viewDidLoad() { super.viewDidLoad() // Instantiate sign-in UI from the SDK library if !AWSSignInManager.sharedInstance().isLoggedIn { AWSAuthUIViewController .presentViewController(with: self.navigationController!, configuration: nil, completionHandler: { (provider: AWSSignInProvider, error: Error?) in if error != nil { print("Error occurred: \(String(describing: error))") } else { // Sign in successful. } }) } // . . . } }

Run the App and Validate Results

Rebuild the project and run in the Simulator. You should see a sign-in screen. Choose the Create new account button to create a new account. Once the information is submitted, you will be sent a confirmation code via email. Enter the confirmation code to complete registration, then sign-in with your new account.


Use Amazon WorkMail as a test email account

If you do not want to use your own email account as a test account, create an Amazon WorkMail service within AWS for test accounts. You can get started for free with a 30-day trial for up to 25 accounts.

            Demo of Notes tutorial app with user sign-in added.

Next steps