AWS 入門
建置 iOS 應用程式
使用 AWS Amplify 建立簡單的 iOS 應用程式
第二單元︰初始化 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 組態檔案新增至我們的專案
使用搜尋工具,在專案目錄的根目錄下找到 awsconfiguration.json 和 amplifyconfiguration.json。將其拖放至您的 Xcode 專案:
b.在執行時間載入 Amplify 類別
我們來建立一個後端類別,對程式碼進行分組,以便與我們的後端互動。我們使用 singleton 設計模式,使其可輕鬆用於應用程式,並確保 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)") } } }
應用程式完成啟動後,我們初始化 singleton 後端物件。
開啟 AppDelegate.swift 檔案並新增 Backend.initialize(); in the application(:didFinishLaunchingWithOptions:) 方法,如下所示:
// 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。應不會發生錯誤。