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 업데이트(Update 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에서 workspace를 열려면 다음 명령을 실행합니다.
xed .
-
런타임 시 Amplify 초기화
런타임 시 Amplify 라이브러리에는 CLI를 통해 생성된 amplify 구성 파일이 필요합니다.
a. 프로젝트에 Amplify 구성 파일 추가
파인더를 사용하여 프로젝트 디렉터리의 루트에서 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)") } } }
애플리케이션이 시작되면 싱글톤 백엔드 객체를 초기화합니다.
다음과 같이 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를 입력합니다. 오류가 발생하지 않아야 합니다.