Build an iOS Application

Create a simple iOS application using AWS Amplify

Module 2: Initialize Amplify

In this module, you will install and configure the Amplify CLI

Overview

Now that we have created an iOS application, we want to continue development and add new features.

To start to use AWS Amplify in your application, you must install the Amplify command line, initialize the Amplify project directory, configure your project to use the Amplify libraries, and initialize Amplify libraries at runtime.

What you will accomplish

In this module, you will:
  • Initialize a new Amplify project
  • Add Amplify libraries in your project
  • Initialize Amplify libraries at runtime

Key concepts

Amplify CLI – The Amplify CLI allows you to create, manage, and remove AWS services directly from your terminal.

Amplify libraries – The Amplify libraries allow you to interact with AWS services from a web or mobile application.

 Time to complete

10 minutes

 Services used

Implementation

Install Amplify CLI

To install AWS Amplify CLI, open a Terminal, and enter the following command:

## Install Amplify CLI
curl -sL https://aws-amplify.github.io/amplify-cli/install | bash && $SHELL

## Verify installation and version
amplify --version

# 10.5.1

Initialize an Amplify backend

To create the basic structure of our backend, we first need to initialize the Amplify project directory and create our cloud backend.

Open a Terminal and change directories to your project. For example, if you created your project in the folder ~/Developer, you can enter:

cd ~/Developer/iOS\ Getting\ Started

Verify you are in the correct directory. It should look like this:

➜  iOS Getting Started git:(master) ✗ ls -al
total 32
drwxr-xr-x   9 stormacq  admin   288 Jul  8 15:09 .
drwxr-xr-x  17 stormacq  admin   544 Jul  6 16:20 ..
-rw-r--r--@  1 stormacq  admin  6148 Jul  8 15:06 .DS_Store
drwxr-xr-x   9 stormacq  admin   288 Jul  6 16:12 iOS Getting Started
drwxr-xr-x@  5 stormacq  admin   160 Jul  8 15:09 iOS Getting Started.xcodeproj

Initialize the Amplify project structure and configuration file. Run the following command:

amplify init

? Enter a name for the project (iOSGettingStarted): accept the default, press enter
The following configuration will be applied:
Project information
| Name: iOSGettingStarted
| Environment: dev
| Default editor: Visual Studio Code
| App type: ios
? Initialize the project with the above configuration? Yes, press enter
Using default provider  awscloudformation, press enter
? Select the authentication method you want to use: AWS profile, press enter
? Please choose the profile you want to use: default, press enter

You can create a profile using AWS CLI using aws configure --profile <name> if you don't have one yet.

Amplify initializes your project in the cloud, it might take a few minutes. After a few minutes, you should see a message like:

✔ Successfully created initial AWS cloud resources for deployments.
✔ Initialized provider successfully.
Initialized your environment successfully.

Your project has been successfully initialized and connected to the cloud!

Add Amplify libraries to your project

Switch back to Xcode. Select File > Add Packages...

Enter the Amplify Libraries for Swift GitHub repo URL ( https://github.com/aws-amplify/amplify-swift) into the search bar and hit Enter. Wait for the result to load. You'll see the Amplify Libraries for Swift repository rules for which version of Amplify you want Swift Package Manager to install. Choose the dependency rule Up to Next Major Version, as it will use the latest compatible version of the dependency that can be detected from the main branch, then click Add Package.

Lastly, choose which of the libraries you want added to your project. For this tutorial, select Amplify, then choose Add Package.

Initialize Amplify at runtime

At runtime, the Amplify libraries require the Amplify configuration files generated by the CLI.

a. Add the Amplify Configuration Files to our Project.

Using the Finder, locate awsconfiguration.json and amplifyconfiguration.json at the root of your project directory. Drag and drop them into your Xcode project:

b. Load the Amplify classes at runtime.

Let's create a backend class to group the code to interact with our backend. We will use a singleton design pattern to make it easily available through the application and to ensure the Amplify libraries are initialized only once.

The class initializer takes care of initializing the Amplify libraries.

Create a new Swift text file Backend.swift, add it to your Xcode project (CTRL-N), and add this code:

import UIKit
import Amplify

class Backend {
    static let shared = Backend()
    static func initialize() -> Backend {
        return .shared
    }
    private init() {
      // initialize amplify
      do {
        try Amplify.configure()
        print("Initialized Amplify");
      } catch {
        print("Could not initialize Amplify: \(error)")
      }
    }
}

We initialize our singleton backend object when the application finishes launching.

Open the <PROJECT_NAME>App.swift file and add Backend.initialize() in the init() method, just like this:

 // inside the <PROJECT_NAME>App.swift file 
    init() {
        // initialize Amplify
        Backend.initialize()

    return true
}

Verify your setup

To verify everything works as expected, build the project. Select the Product menu and then select Build, or press ⌘B. There should be no error.

Conclusion

You have initialized the Amplify project and are now ready to start adding features! We will add an entire user authentication flow with just a few lines of code in the next module.

Was this page helpful?

Add user authentication