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..

Using 3rd-party libraries using CocoaPods

What is Cocoapods?

CocoaPods is the dependency manager for Swift and Objective-C Cocoa projects. It has almost ten thousand libraries and can help you scale your projects elegantly.

Reference: https://cocoapods.org/

CocoaPods

CocoaPods

Open your terminal and type this command:

sudo gem install cocoapods
Installation CocoaPods via terminal.

Installation CocoaPods via terminal.

Init your xcode project.

pod init

Search for pod file in your xcode project (podfile). Then list the dependencies in a text file named Podfile in your Xcode project directory:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
  pod 'AFNetworking', '~> 2.5'
  pod 'ORStackView', '~> 2.0'
  pod 'SwiftyJSON', '~> 2.1'
end

Now you can install the dependencies in your project:

pod install
pod install

pod install

Open your *.xcworkspace.

HelloWorld.xcworkspace

HelloWorld.xcworkspace

You can verify it here to make sure your dependencies already installed.

Pod

Pod

Develop a simple iOS mobile app (toDoList) using Swift – Part 1

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

swift programming language

swift programming language

Here are some of features:

  1. Modern
  2. Designed for Safety
  3. Fast and Powerful
  4. Interactive Playgrounds
  5. Ready Today
  6. Read-Eval-Print-Loop (REPL)

Reference: https://developer.apple.com/swift/

Getting Started.

Before we start this tutorial, make sure you already setup your machine (mac) with these tools:

  1. Xcode 6.3.2 GM seed – https://developer.apple.com/xcode/downloads/ (More stable than Xcode 6.3.1).

Open Xcode and create a new Single View Application. For product name, use RevivalxSwiftToDoList 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

RevivalxSwiftToDoList

RevivalxSwiftToDoList

Install CocoaPods using terminal. What is CocoaPods?

CocoaPods manages library dependencies for your Xcode projects.

The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.

Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.

Reference: https://guides.cocoapods.org/using/getting-started.html

Open your terminal and type this command.

sudo gem install cocoapods
sudo gem install cocoapods

sudo gem install cocoapods

Go to your directory and initialize your pod using this command.

cd /Users/MNurdin/Documents/iOS/RevivalxSwiftToDoList
pod init
pod init

pod init

Open your finder and edit pod file (Podfile) with these one.

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

pod 'HanekeSwift'
pod 'Parse'
pod 'ParseFacebookUtils'
pod 'ParseUI'

target 'RevivalxSwiftToDoList' do

end

target 'RevivalxSwiftToDoListTests' do

end

The reasons why we need these libraries because

Go back to your terminal and run pod install to install all the dependencies. It takes a few of minutes to download.

pod install
Pod install.

Pod install.

After finish downloading, open RevivalxSwiftToDoList.xcworkspace file, not RevivalxSwiftToDoList.xcodeproj because we want to use the pod.

Storyboards and interface builder

Set to Compact Width | Any Height.

Compact Width | Any Height

Compact Width | Any Height

Delete ViewController.swift and ViewController on the Main.Storyboard. Drag a Table View Controller and View Controller to Main.Storyboard.

Main.Storyboard

Main.Storyboard

Select Table View Controller and navigate to Attributes inspector. Tick Is Initial View Controller.

Attributes inspector

Attributes inspector

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 the Navigation Item under the Table View Controller, and in the Attributes Inspector type To Do List as the Title.

Navigation Item

Navigation Item

From the Object Library, drag a Bar Button Item to the right of the navigation bar on the Table View Controller. Select the Bar Button Item (which has a default value of “Item”) and in the Attributes Inspector, in the Identifier option, choose Add. This will change the button to the add symbol (+).

Bar Button Item

Bar Button Item

Select the Table View Controller, press control button and drag to View Controller like image below. We added a segue.

Segue

Segue

Name it MySegue in Show the Attributes Inspector > Identifier.

Storyboard Segue

Storyboard Segue

Result

Main.Storyboard

Main.Storyboard

Continue on next post, http://blog.revivalx.com/2015/08/12/develop-a-simple-ios-mobile-app-todolist-using-swift-part-2/.