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 ライブラリにより、ウェブまたはモバイルアプリケーションから 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 を更新して次のポッドが含まれるようにしてから、プラットフォームを更新します。
# 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 クラスを読み込む
Backend クラスを作成して、バックエンドとやり取りするコードをグループ化しましょう。Singleton デザインパターンを使用してアプリケーションで簡単に利用できるようにし、Amplify ライブラリが 1 回だけ初期化されるようにします。
クラス初期化子は、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 Backend オブジェクトを初期化します。
AppDelegate.swift ファイルを開き、Backend.initialize() を追加します。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 を押します。何のエラーも発生しないはずです。