Upload and retrieve photos using Amazon iOS SDK and Swift – Part 1

Here a cool tutorial how to upload and retrieve photos using Amazon iOS SDK and Swift. You can learn the foundation here, https://aws.amazon.com/mobile/sdk/.

Amazon Web Services.

Amazon Web Services.

Getting Started.

Before we start the tutorial, I assumed you already registered your account in AWS. Then setup Amazon Cognito and S3 here

Open your Xcode 7.1. Open Xcode and create a new Single View Application. For product name, use RevivalxSwiftAmazonS3 and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Single View Application

Single View Application

RevivalxSwiftAmazonS3

RevivalxSwiftAmazonS3

Install CocoaPods in your machine. Follow this tutorial for more details, http://blog.revivalx.com/2015/06/25/using-3rd-party-libraries-using-cocoapods/. Before you execute pod install command, replace with this code and run pod install command.

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'
use_frameworks!

pod 'Alamofire', '~> 1.2'
pod 'HanekeSwift'
pod 'SwiftyJSON', '~> 2.2.0'
pod 'AWSS3'

target 'RevivalxSwiftAmazonS3' do

end

target 'RevivalxSwiftAmazonS3Tests' do

end
CocoaPods - pod install

CocoaPods – pod install

Go to Main.Storyboard and set to Compact Width | Any Height.

Compact Width | Any Height

Compact Width | Any Height

Go to UIViewController in Main.Storyboard and remove UIView. Then go to Show the Object Library and add UICollectionView in UIViewController.

 

UIViewController - Remove UIView

UIViewController – Remove UIView

 

UIViewController - Add UICollectionView

UIViewController – Add UICollectionView

Hold control button and drag from UICollectionView to yellow square icon like image below. Make sure you select both dataSource and delegate.

 

UIViewController - DataSource and delegate outlet.

UIViewController – DataSource and delegate outlet.

Select the Table View Controller again and in the menu go to Editor > Embed In >Navigation Controller. This adds a Navigation Controller to the storyboard with an initial scene.

Editor > Embed In > Navigation Controller

Editor > Embed In > Navigation Controller

Select RevivalxSwiftAmazonS3. Go to File > New > File… to create a new file. Select Cocoa Touch Class under iOS > Source and click Next.

Choose a template for your new file

Choose a template for your new file

Select UICollectionViewCell from Subclass of. Click Next and Create after you select file directory to save.

UICollectionViewCell

UICollectionViewCell

Select one more time on RevivalxSwiftAmazonS3 group. Go to File > New > File… to create a new file. This time we need to create a bridging header file to user Amazon SDK. Select Objective-C File under iOS > Source and click Next.

Objective-C File

Objective-C File

Name it CustomObject. Click Next and Create.

xcode6 - new file

xcode6 – new file

When adding your .m file, you’ll likely be hit with a prompt that looks like this below and click Yes.

Objective-C bridging header

Objective-C bridging header

Your CustomObject.m and SimpleiOSApp-Bridging-Header.h have been created. Add another .h file and name it CustomObject.hFile > New > File. Select iOS >Source > Header File. Click Next . Name it CustomObject and click Create.

Header File

Header File

Now add Class to Bridging-Header. In YourProject-Bridging-Header.h (For this tutorial is RevivalxSwiftAmazonS3-Bridging-Header.h):

#import <AWSS3/AWSS3.h>;

Copy Cognito Identity Pool Id from Amazon Cognito. Make sure you know region type for Amazon Cognito and S3. Open AppDelegate.swift and update the with this code.

import UIKit
import AWSS3

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var cognitoIdentityPoolId: String = "ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.APNortheast1, identityPoolId: cognitoIdentityPoolId)
        
        let defaultServiceConfiguration = AWSServiceConfiguration(
            region: AWSRegionType.APSoutheast1, credentialsProvider: credentialsProvider)
        
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration
        
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
    }

    func applicationDidEnterBackground(application: UIApplication) {
    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
    }

    func applicationWillTerminate(application: UIApplication) {
    }
}

Continue..