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 you have created an iOS application, you will 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
- Initialize a new Amplify project
- Add Amplify libraries in your project
- Initialize Amplify libraries at runtime
Key concepts
Amplify CLI – Using the Amplify CLI you can create, manage, and remove AWS services directly from your terminal.
Amplify libraries – Using Amplify libraries you can 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 Terminal, and enter the following command:
curl -sL https://aws-amplify.github.io/amplify-cli/install | bash && $SHELL
Configure it to connect to your AWS account by running the following command:
amplify configure
Follow the steps as instructed. You can find a more detailed guide here.
-
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 the Terminal, navigate to the root directory of your project and run the following command:
amplify init
You will be asked to enter a name for the project. Keep the default name, and then validate that the information is correct. The following code is an example.
? Enter a name for the project (GettingStarted) The following configuration will be applied: Project information | Name: GettingStarted | Environment: dev | Default editor: Visual Studio Code | App type: ios
Proceed with the remaining steps:
Initialize the project with the above configuration? (Y/n) Y Select the authentication method you want to use: (Use arrow keys) AWS profile Please choose the profile you want to use default
This will provision the resources in the backend and might take a few minutes. Once it's done, you will see the following information:
Deployment state saved successfully. ✔ Initialized provider successfully. ✅ Initialized your environment successfully. ✅ Your project has been successfully initialized and connected
-
Add Amplify libraries to your project
Switch back to Xcode. Select File and choose Add Package Dependencies...
Enter the Amplify Libraries for Swift GitHub repo URL (https://github.com/aws-amplify/amplify-swift) into the search bar, and press Enter.
Make sure that Up to Next Major Version is selected from the Dependency Rule dropdown, and select Add Package.
Once the libraries are fetched, you will be asked to select which ones you wish to add to your target.
In the drop down, next to Amplify, choose GettingStarted.
Select None in for the rest of the Package Products in the Add to Target section, and choose Add Package.
-
Initialize Amplify at runtime
Open the GettingStartedApp.swift file and replace its content with the following information:
import Amplify import SwiftUI @main struct GettingStartedApp: App { init() { do { try Amplify.configure() print("Initialized Amplify"); } catch { print("Could not initialize Amplify: \(error)") } } var body: some Scene { WindowGroup { NotesView() } } }
-
Verify your setup
To verify everything works as expected, build the project. Select the Product menu and then select Build, or press Cmd + B. There should be no error.
Conclusion
You have initialized the Amplify project and are now ready to start adding features! In the next module, we will add an entire user authentication flow with just a few lines of code.