I decided to develop an application linked with AWS in the project, but unexpectedly there were few articles to refer to, so I had a hard time, so I summarized the procedure. This time I will try to get the table name from DynamoDB.
Follow the README procedure in aws-amplify/aws-sdk-ios.
First, install cocoapods.
Terminal
$ gem install cocoapods
$ pod setup
Pod init directly under the project directory
Terminal
$ pod init
Since the podfile has been created, I will describe the AWS service to use
Podfile
target 'aws-sdk' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'AWSDynamoDB'
end
After installing the pod, reopen Xcode with xed .
.
Terminal
$ pod install --repo-uodate
$ xed .
You need to authenticate with AWS to connect to AWS. As far as I have investigated, there are the following two types.
I tried both, but Cognito's authentication was cumbersome to set up, so I decided to authenticate with method 2. Please refer to the following for issuing the Key.
How do I create an AWS access key? Create Qiita AWS Access Key
Authentication is set in AppDelegate.swift.
AppDelegate.swift
import UIKit
import AWSCore
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Add the following
let accessKey = Constans.accessKey
let secretKey = Constans.secretKey
let provider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
let configuration = AWSServiceConfiguration(
region: AWSRegionType.APNortheast1, //Please change the region as appropriate
credentialsProvider: provider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
return true
}
}
The management of the accessKey and secretKey of AWSStaticCredentialsProvider (accessKey: accessKey, secretKey: secretKey)
is left to you individually. (Please be careful not to publish it.)
In my case, I managed it with cocoapods-keys in the case, but this time I created a constant class simply. The created file is set to .gitignore so that it will not be pushed. orta/cocoapods-keys
Constants.swift
import Foundation
class Constans {
static let accessKey = "AWS access-key"
static let secretKey = "AWS secret-key"
}
ViewController.swift
import UIKit
import AWSDynamoDB
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
fetchDynamoDBTableName()
}
private func fetchDynamoDBTableName() {
let dynamoDB = AWSDynamoDB.default()
let listTableInput = AWSDynamoDBListTablesInput()
dynamoDB.listTables(listTableInput!).continueWith { (task:AWSTask<AWSDynamoDBListTablesOutput>) -> Any? in
if let error = task.error as? NSError {
print("Error occurred: \(error)")
return nil
}
let listTablesOutput = task.result
for tableName in listTablesOutput!.tableNames! {
print("\(tableName)")
}
return nil
}
}
}
When you build with this, you should see the table name that exists in DynamoDB on the console. If there are multiple tables, multiple are displayed. I described the method in ViewController, but since ViewController becomes Fat, I think it is better to create a class for DynamoDM.
For your reference. https://github.com/shungo0525/aws-sdk
This time I only get the table name of DynamoDB, but I would like to post about the acquisition of DB data and the cooperation with S3.
Recommended Posts