Getting started

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.

Install Dependencies

After initialization in your project directory with amplify init, edit your Podfile with the following:

1target 'MyApp' do ##Replace MyApp with your application name
2 use_frameworks!
3 pod 'AWSMobileClient' # Required dependency
4 pod 'AWSAuthUI' # Optional dependency required to use drop-in UI
5 pod 'AWSUserPoolsSignIn' # Optional dependency required to use drop-in UI
6end

Next run the following command:

1pod 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 choose Finish.

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:

1amplify 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:

1amplify 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:

1{
2 "IdentityManager": {
3 "Default": {}
4 },
5 "CredentialsProvider": {
6 "CognitoIdentity": {
7 "Default": {
8 "PoolId": "XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab",
9 "Region": "XX-XXXX-X"
10 }
11 }
12 },
13 "CognitoUserPool": {
14 "Default": {
15 "PoolId": "XX-XXXX-X_abcd1234",
16 "AppClientId": "XXXXXXXX",
17 "Region": "XX-XXXX-X"
18 }
19 }
20}

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:

1import AWSMobileClient
2
3@UIApplicationMain
4class AppDelegate: UIResponder, UIApplicationDelegate {
5
6 func application(
7 _ application: UIApplication,
8 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
9 ) -> Bool {
10
11 // Initialize AWSMobileClient singleton
12 AWSMobileClient.default().initialize { (userState, error) in
13 if let userState = userState {
14 print("UserState: \(userState.rawValue)")
15 } else if let error = error {
16 print("error: \(error.localizedDescription)")
17 }
18 }
19
20 return true
21 }
22 ...
23}

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:

1import AWSMobileClient
2
3@UIApplicationMain
4class AppDelegate: UIResponder, UIApplicationDelegate {
5 ...
6 override func viewDidLoad() {
7 super.viewDidLoad()
8 switch( AWSMobileClient.default().currentUserState) {
9 case .signedIn:
10 DispatchQueue.main.async {
11 print("Logged In")
12 }
13 case .signedOut:
14 DispatchQueue.main.async {
15 print("Signed Out")
16 }
17 default:
18 AWSMobileClient.default().signOut()
19 }
20 }
21}

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.