开始使用 AWS
构建 iOS 应用程序
使用 AWS Amplify 创建简单的 iOS 应用程序
模块 2:初始化 Amplify
在此模块中,您将安装并配置 Amplify CLI。
简介
现在我们已创建一个 iOS 应用程序,我们想要继续开发并添加新功能。
要开始在应用程序中使用 AWS Amplify,必须安装 Amplify 命令行,初始化 Amplify 项目目录,将项目配置为使用 Amplify 库,并在运行时初始化 Amplify 库。
您将学到的内容
- 初始化新的 Amplify 项目
- 将 Amplify 库添加到项目中
- 在运行时初始化 Amplify 库
重要概念
Amplify CLI – Amplify CLI 可使您直接从您的终端创建、管理和删除 AWS 服务。
Amplify 库 – 您可以使用 Amplify 库通过 Web 应用程序或移动应用程序使用 AWS 服务。
完成时间
10 分钟
使用的服务
实施
-
安装 Amplify CLI
AWS Amplify CLI 取决于 Node.js,安装 Node.js 前请参考简介中的先决条件部分
要安装 AWS Amplify CLI,请打开一个终端,然后输入以下命令:
## Install Amplify CLI npm install -g @aws-amplify/cli ## Verify installation and version amplify --version # Scanning for plugins... # Plugin scan successful # 4.27.2
-
初始化 Amplify 后端
要创建后端基本结构,首先需要初始化 Amplify 项目目录并创建云后端。
打开一个终端并将目录更改为您的项目。例如,如果您在文件夹 ~/Developer 中创建项目,则可以键入:
cd ~/Developer/iOS\ Getting\ Started
验证您是否在正确的目录中,它应该如下所示:
➜ 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
初始化 Amplify 项目结构和配置文件。运行以下命令:
amplify init ? Enter a name for your project (iOSGettingStarted): accept the default, press enter ? Enter a name for the environment (dev): accept the default, press enter ? Choose your default editor: use the arrow key to select your favorite text editor an press enter ? Choose the type of app that you're building: iOS is already selected, press enter ? Do you want to use an AWS profile?, Y, press enter ? Please choose the profile you want to use: use the arrow keys to select your profile and press enter.
如果没有配置文件,可使用 AWS CLI 键入命令 aws configure --profile <name> 创建一个。
Amplify 会在云中初始化您的项目,这可能需要几分钟时间。几分钟后,您应该会看到如下消息:
✔ 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!
-
将 Amplify 库添加到项目中
在开始此步骤之前,请确保关闭 Xcode。
a.打开一个终端并将目录更改为您的项目。例如,如果您在文件夹 ~/Developer 中创建项目,则可以键入:
cd ~/Developer/iOS\ Getting\ Started
验证您是否在正确的目录中,它应该如下所示:
➜ 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 6 stormacq admin 192 Jul 8 15:26 amplify -rw-r--r-- 1 stormacq admin 64 Jul 8 15:26 amplifyconfiguration.json -rw-r--r-- 1 stormacq admin 116 Jul 8 15:26 awsconfiguration.json 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
b.要使用 CocoaPods 软件包管理器初始化项目,请执行以下命令。
## is Xcode really closed ? ## Initialize the Amplify Project pod init
完成此操作后,您应该看到一个名为 Podfile 的新建文件。此文件用于描述您的项目所依赖的软件包。
c.更新 Podfile 以包含以下 Pod 并更新平台:
# you need at least version 13.0 for this tutorial, more recent versions are valid too platform :ios, '13.0' target 'iOS Getting Started' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for getting started pod 'Amplify', '~> 1.0' # required amplify dependency pod 'Amplify/Tools', '~> 1.0' # allows to call amplify CLI from within Xcode end
d.要将 Amplify 库下载并安装到您的项目中,请执行以下命令:
pod install --repo-update
e.完成此操作后,您现在应该看到一个名为 iOS Getting Started.xcworkspace 的文件。从现在开始,您需要使用此文件,而不是 iOS Getting Started.xcodeproj 文件。
您的目录应该如下所示:
➜ 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 6 stormacq admin 192 Jul 8 15:26 amplify -rw-r--r-- 1 stormacq admin 64 Jul 8 15:26 amplifyconfiguration.json -rw-r--r-- 1 stormacq admin 116 Jul 8 15:26 awsconfiguration.json -rw-r--r-- 1 stormacq admin 394 Jul 8 15:09 Podfile -rw-r--r-- 1 stormacq admin 359 Jul 8 15:09 Podfile.lock drwxr-xr-x 8 stormacq admin 256 Jul 8 15:09 Pods 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 drwxr-xr-x 3 stormacq admin 96 Jul 8 15:09 iOS Getting Started.xcworkspace
要在 Xcode 中打开工作区,请执行以下命令:
xed .
-
在运行时初始化 Amplify
在运行时,Amplify 库需要由 CLI 生成的 Amplify 配置文件
a.将 Amplify 配置文件添加到项目中
使用 Finder 在项目目录的根目录下找到 awsconfiguration.json 和 amplifyconfiguration.json。将其拖放到 Xcode 项目中:
b.在运行时加载 Amplify 类
我们来创建一个后端类对代码进行分组,以便与后端交互。我们使用单例设计模式,使其通过应用程序轻松可用,并确保 Amplify 库仅初始化一次。
类初始化程序负责初始化 Amplify 库。
新建一个 swift 文本文件 Backend.swift,将其添加到您的 Xcode 项目 (CTRL-N) 并添加以下代码:
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)") } } }
应用程序启动后,初始化单例 Backend 对象。
打开 AppDelegate.swift 文件并在 application(:didFinishLaunchingWithOptions:) 方法中添加 Backend.initialize();,如下所示:
// inside the AppDelegate.swift file func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // initialize Amplify let _ = Backend.initialize() return true }
-
验证您的设置
要验证一切是否都按预期运行,请构建项目。单击 Product 菜单,然后选择 Build,或按 ⌘B。应该不会出现错误。