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

Arduino Uno computer architectures

Arduino Uno is a microcontroller based on the ATmega328P, it’s a heart of Arduino board which is the largest chip on the board. The chip’s memory contain 32 kilobytes and operates at clock speed 16 megahertz.

Arduino Uno

Arduino Uno

When the power turn off, the program will remain in the chip because it’s a flash memory. The operation voltage is 5 volts. This board has reset button to restart the program all over the again. USB input can be use to connect with external devices that support USB port or connect with the power supply. Besides that the power source can obtain from 7-12V DC input connector to power this board. It has 14 digital I/O pins that provide between 0 and 5 volts and 6 analog input pins that accept 0-5 volts input. There also have access to 3.3 volts and 5 volts pins. You also can connect with custom circuits such as breadboard through the female headers (digital and analog pins).

There also a thing called ICSP (In Circuit Serial Connector Programmer), normally have an external piece of hardware that use to program the microcontroller to download the software, written in C or assembly language (ASM). The microcontroller chip can be preprogrammed with a Bootloader program in the microcontroller memory. The small piece of chip (smaller than the larger one) that adds USB functionality to the ATmega328P chip.

Arduino board specs.

Arduino board specs.

P.S: For those interested to build DIY projects, can buy online from here, http://my.element14.com/arduino.

Arduino & Genuino Products

Arduino, an open source microcontroller that allows developers use of it for DIY (Do It Yourself) and custom projects that can relate with real life daily application. There are a lot of thousands of projects already build using this microcontroller. There are five types of Arduino boards are entry level, enhanced features, internet of things, wearable and 3D printing.

Arduino - Products.

Arduino – Products.

For entry level is suitable for those want to make as their first learning in microcontroller world. There are Arduino/Genuino Uno, Arduino Pro, Arduino Pro Mini, Arduino/Genuino Micro, Arduino Nano, Arduino/Genuino Starter Kit, Arduino Basic Kit and Arduino Motor Shield. Similar to Raspberry PI, a rival to Arduino with is good for entry level. Later we will discuss more details about the comparision between these two. For entry level, mostly Arduino Uno is the most popular compare to the others because the price is cheap and support most of the functions and external device integration such as camera, ultra sonic and DC motor. Besides that it’s really suitable for students for those have basic programming in C++ and it’s easy to use.

If the developers want more advanced functionalites, features and faster performance, they can select for enhanced features type. There are Arduino/Genuino Mega, Arduino Zero, Arduino Due and Arduino Proto Shield. The major differences between these two Arduino are number of pins and memory size. Enhanced features type got a lot number of pins and large memory compare to entry level less number of pins and small memory. You can integrate a lot of external devices such as camera, ultra sonic and DC motor on enhanced features type. Besides that the performance can be more better than entry level type during program execution.

Compare to internet of things, this type can connect to internet or clouds that can exchange the data between internet and the embedded device (Arduino). It can perform machine to machine communications (M2M) and apply it to smart cities. The microcontroller can connect to internet using GSM, ethernet and Wifi. Arduino Yun and Arduino Ethernet Shield support for ethernet connection (LAN or called local area network), Arduino GSM Shield only support for GSM connection (only GPRS, not 3G,4G or LTE) and Arduino Wifi Shield 101 support for wireless connection.

For those interested to wearable technology, they can choose wearable type. There are five microcontrollers, Arduino Gemma, Lilypad Arduino USB, Lilypad Arduino Main Board, Lilypad Arduino Simple and Lilypad Arduino Simple Snap. What actually is wearable? Wearable is a clothing and accessories that integrated with computer and advanced electronic technologies like we seen today. There are a lot of existing wearable technologies out there such as iWatch, android wear, Pabble and Jawbone (3/24). It can be similar with internet of things because it’s connected to the internet but it more on fashion or accessories perspective, it’s wearable.

If you a hobbyist in 3D printing, 3D printing type is a best selection because it’s only one microcontroller that support 3D printing features. It’s called Materia 101. It uses an Arduino Mega2560 with a white shield for controlling 3D printers. It’s open source, not for commercial purpose as all Arduino products.

P.S: For those interested to build DIY projects, can buy online from here, http://my.element14.com/arduino.

iOS Swift Basics Tutorial: Protocols and Delegates

Here a cool tutorial protocols and delegates using Swift. This link will explain more details about the delegate, https://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/.

Getting started.

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

  1. Xcode 7.1: https://developer.apple.com/xcode/

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

RevivalxSwiftProtocolDelegate

RevivalxSwiftProtocolDelegate

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

Compact Width | Any Height

Compact Width | Any Height

Select View Controller in storyboard and select Editor > Embed In > Navigation Controller.

Embed in Navigation Controller.

Embed in Navigation Controller.

Navigation Controller.

Navigation Controller.

Go to Object Library and drag/drop a new View Controller into storyboard.

Main.Storyboard.

Main.Storyboard.

Go to Object Library again and drag/drop Bar Button Item into first view controller. Change the Bar Button Item icon to plus icon. Then press control button and at the same time drag the mouse/pointer to second view controller. Select show under Action Segue. Finally add Navigation Item into second view controller and name it for both view controllers.

Main.Storyboard.

Main.Storyboard.

Drag and drop Label and TextField from Object Library into both view controllers. Make sure it similar with example below. Don’t forget to set autolayout for each label and textfield. If you not really understand about the autolayout, feel free to read this tutorial. http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1

Main.Storyboard.

Main.Storyboard.

Create a new ViewController.swift (UIViewController).

Xcode 6.4 – New File.

Xcode 6.4 – New File.

Cocoa Touch Class

Cocoa Touch Class

UIViewController

UIViewController

Project navigator.

Project navigator.

Select second view controller and make sure you attach SecondViewController into each controllers in Identity inspector.

Identity inspector -UIViewController

Identity inspector -UIViewController

Select the Assistant Editor and open SecondViewController.swift. Ctrl and drag from the Text Field to the ViewController class to create the following outlet.

TextField outlet.

TextField outlet.

Ctrl and drag from the Button and create the following action.

Button outlet.

Button outlet.

Open ViewController.swift. Ctrl and drag from the Label to the ViewController class to create the following outlet.

Label outlet.

Label outlet.

Select Attribute inspector and name it as SecondSegue in Identifier field.

Storyboard segue.

Storyboard segue.

Go to the SecondViewController.swift file and replace with this code.

import UIKit

protocol DataEnteredDelegate{
    func userDidEnterInformation(info: NSString)
}

class SecondViewController: UIViewController {

    @IBOutlet weak var txtData: UITextField!
    var delegate:DataEnteredDelegate? = nil
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func sendData(sender: AnyObject) {
        if (delegate != nil){
            let data: NSString = txtData.text
            
            delegate!.userDidEnterInformation(data)
            self.navigationController?.popViewControllerAnimated(true)
        }
    }
}

Open ViewController.swift and update the with this code.

import UIKit

class ViewController: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var lblResult: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    func userDidEnterInformation(info: NSString) {
        lblResult.text = info as String
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == &quot;SecondSegue&quot; {
            let secondViewController: SecondViewController = segue.destinationViewController as! SecondViewController
            secondViewController.delegate = self
        }
    }
}

Run your project. Feel free to check out my source code here, https://github.com/datomnurdin/RevivalxSwiftProtocolDelegate.