Getting started
Install Dependencies
After initialization in your project directory with amplify init
, edit your Podfile
with the following:
target 'MyApp' do ##Replace MyApp with your application name use_frameworks! pod 'AWSMobileClient' # Required dependency pod 'AWSAuthUI' # Optional dependency required to use drop-in UI pod 'AWSUserPoolsSignIn' # Optional dependency required to use drop-in UIend
Next run the following command:
pod install --repo-update
Open the .xcworkspace file for your project (close the .xcodeproj file if you already have it open). The CLI will create the awsconfiguration.json
file in your project directory. In the Finder, drag awsconfiguration.json
into Xcode under the top Project Navigator folder (the folder name should match your Xcode project name). When the Options
dialog box appears, do the following:
- Clear the
Copy items if needed
check box. - Choose
Create groups
, and then chooseFinish
.
Build your project once to ensure all frameworks are pulled in and compile.
Automated Setup
Run the following command in your project's root folder:
amplify add auth
If you have previously enabled an Amplify category that uses Auth behind the scenes, e.g. Storage category, you may already have an Auth configuration. In such a case, run
amplify auth update
command to edit your configuration.
The Amplify CLI prompts will help you to customize your auth configuration:
- Customize sign-in/registration flow
- Customize email and SMS messages for Multi-Factor Authentication
- Customize attributes for your users, e.g. name, email
- Enable 3rd party authentication providers like Facebook, Twitter, Google and Amazon
After configuring your Authentication options, update your backend:
amplify push
Lambda Triggers
The CLI allows you to configure Lambda Triggers for your Amazon Cognito User Pool. These enable you to add custom functionality to your registration and authentication flows.
Manual Setup
For manual configuration without the CLI, you must have an awsconfiguration.json
file with the following:
{ "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab", "Region": "XX-XXXX-X" } } }, "CognitoUserPool": { "Default": { "PoolId": "XX-XXXX-X_abcd1234", "AppClientId": "XXXXXXXX", "Region": "XX-XXXX-X" } }}
If you are using both Cognito User and Identity Pools you will need all of the keys mentioned above.
Initialization
Open the AppDelegate.swift
file in your Xcode project, import AWSMobileClient
and add to the application
function:
import AWSMobileClient
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool {
// Initialize AWSMobileClient singleton AWSMobileClient.default().initialize { (userState, error) in if let userState = userState { print("UserState: \(userState.rawValue)") } else if let error = error { print("error: \(error.localizedDescription)") } }
return true } ...}
Build and run your program to see the initialized client in Xcode messages. Since you haven't logged in yet it will print a state of signedOut
. The userState
returns an ENUM which you can perform different actions in your workflow. For example, now add to the viewDidLoad()
function of your ViewController.swift
:
import AWSMobileClient
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { ... override func viewDidLoad() { super.viewDidLoad() switch( AWSMobileClient.default().currentUserState) { case .signedIn: DispatchQueue.main.async { print("Logged In") } case .signedOut: DispatchQueue.main.async { print("Signed Out") } default: AWSMobileClient.default().signOut() } }}
You might leverage the above workflow to perform other actions in the signedIn
case, such as calling GraphQL APIs with AWS AppSync or uploading content with Amazon S3.