App Transport Security(ATS)

Client <-http-> Server
Get in touch with the people running the server

Web Services and API
client(app) HTTP server

Flicker API document
https://www.flickr.com/services/api/

42 *, 43 +, 44,, 45 -, 46.,47/,

https://api.flicker.com/services/rest/?method=*&api_key=*&text=*&extras=url_m

some of API
OpenMenu API, World Bank API, Sunlight API, Gilt API, Riot GAmes API

To use an API:
1. Sign up for an account with the service
2. Register the app
3. [Usually] source authentication process

NSURLSession

network requests are known as “tasks”
->
NSURLSessionTask
Data: into memory as NSData
Download:
Upload: specialized for uploading

Apple Documentation: URLSessionTask
https://developer.apple.com/documentation/foundation/urlsessiontask
Apple Documentation: URLSessionDataTask
Apple Documentation: URLSessionDownloadTask
Apple Documentation: URLSessionDelegate
Apple Documentation: URLSessionDataDelegate
Apple Documentation: URLSessionDownloadDelegate
Apple Documentation: URLSessionTaskDelegate

import UIKit

class ViewController: UIViewController {
	@IBOutlet weak var imageView: UIImageView!

	override func viewDidLoad(){
		super.viewDidLoad()

		let imageURL = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg");

		let task = NSURLSession.sharedSession().dataTaskWithURL(url: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void)
	}
}
		if error == nil {
			let downloadedImage = UIImage(data: data!)
			self.imageView.image = downloadedImage
		}

GET Request

client – Server

Understanding New Concepts

GET request: An HTTP request where a client requests a specified resource from a server.
HTTP method: Another term for the type of HTTP request.
URL: Specifies a location for retrieving data over the network.
Status code: A number returned in response to an HTTP request that indicates the results of the request.
Client: The role that an iPhone play if it is accessing data from the network(ex. downloading images from Facebook)

import UIKit

class ViewController: UIViewController {
	@IBOutlet weak var imageView: UIImageView!

	override func viewDidLoad(){
		super.viewDidLoad()

		let imageURL = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg");

		NSURLSession.sharedSession()
	}
}

Which apps do you use?

Which apps do you use?
facebook, youtube, facebook messenger, instagram, facetime, snapchat, google maps, pandora maps, pandora radio, twitter, google

“the network” to refer to the internet—the global system of interconnected computer networks that many of us interact with everyday.

Data <-http-> Data

protocol
– a way of communicating
– standard operating procedure
– the rules for communication

Getting Data with HTTP
-> http get request

Table views

Delegate Pattern
Controller, Protocol, View

we use the most to customize the textfield.
textField(_:shouldChangeCharactersInRange: replacementString:)

An array of structs

struct Album {
	let name:String
	let image:UIImage
	let count:Int
}

An array of dictionaries

let albums = [
	["name": "Camera Roll", "imageName": "img1", "count": 5],
	["name": "Recently Deleted", "imageName": "img2", "count": 0],
]

Generating a meme object

func save(){
	let meme = Meme(topText: topTextField.text!, bottomText: bottomTextField.text!, originalImage: imageView.image!, memedImage: memedImage)
}

func generateMemedImage() -> UIImage {
	UIGraphicsBeginImageContext(self.view.frame.size)
	view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
	let memedImage:UIImage = UIGrphicsGetImageFromCurrentImageContext()!
	UIGraphicsEndImageContext()

	return memedImage
} 

What about the camera?

@IBAction func pickAnImageFromAlbum(_ sender: Any){
	let imagePicker = UIImagePickerController()
	imagePicker.delegate = self
	present(imagePicker, animated: true, completion: nil)
}

@IBAction func pickAnImageFromCamera(_ sender: Any){

	let imagePicker = UIImagePickerController()
	imagePicker.delegate = self
	present(imagePicker, animated: true, completion: nil)
}

@IBAction func pickAnImageFromAlbum(_ sender: Any){
	let imagePicker = UIImagePickerController()
	imagePicker.delegate = self
	imagePicker.sourceType = .photoLibrary
	present(imagePicker, animated: true, completion: nil)
}

cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
let memeTextAttributes:[String:Any] = [
	NSStrokeColorAttributeName:
	NSForegroundColorAttributeName:
	NSFontAttributeName: UIFont(name: "HelveticaNeue-CondenseBlack", size: 40)!,
	NSStrokeWidthAttributeName:
]
override func viewWillAppear(_ animated: Bool){
	super.viewWillAppear(animated)
	subscribeTokeyboardNotifications()
}

override func viewWillDisappear(_ animated: Bool){
	super.viewWillDisappear(animated)
	unsubscribeFromKeyboardNotifications()
}

func subscribeToKeyboardNotifications(){
	NotificationCenter.default.addObserver(self, selector: #selector(keboardWillShow(_:)), name: .UIKey)
}

func keyboardWillShow(_ notification:Notification){
	view.frame.origin.y -= getKeyboardHeight(notification)
}

func getKeyboardHeight(_ notification:Notification) -> CGFloat{
	let userInfo = notification.userInfo
	let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
	return keyboardSize.cgRectValue.height
}

Preparing the incoming ViewController

Code: No segue
Code & Segue: Segue is created in Storyboard, action is invoked in code
Seque: No code. set up completely in Storyboard

CurrentVC -> prepareForSeque -> NextVC

override func prepare(for seque: UIStoryboardSegue, sender: Any?)

	if segue.identifier == "rollDice"{
		let controller = segue.destination as! DiceViewController

		controller.firstValue = randomDiceValue()
		controller.secondValue = randomDiceValue()
	}
self.textField1.delegate = emojiDelegate
self.textField2.delegate = colorizerDelegate
self.textField3.delegate = self
@IBAction func pickAnImage(_ sender: Any){

	let imagePicker = UIImagePickerController()
	imagePicker.delegate = self
	present(imagePicker, animated: true, completion: nil)
}

RollViewController

import UIKit

class RollViewController: UIViewController {

	func randomDiceValue() -> Int {
		let randomValue = 1 + arc4random() % 6

		return Int(randomValue)
	}

	@IBAction func rollTheDice(){

		var controller:DiceViewController
		controller = self.storyboard?.instantiateViewControllerWithIdentifier("DiceViewController") as!
		DiceViewController

		controller.firstValue = self.randomDiceValue()
		controller.secondValue = self.randomDiceValue()

		self.presentViewController(controller, animated: true, completion: nil)
	}
}

Presenting the Dice View

import UIKit

class RollViewController: UIViewController {

	func randomDiceValue() -> Int {

		let randomValue = 1 + arc4random() % 6

		return Int(randomDiceValue) 
	}

	@IBAction func rollTheDice(){
		
	}
}
@IBAction func rollTheDice(){

	let controller: DiceViewController
	controller = storyboard?.instantiateViewController(withIdentifier: "DiceViewController") as! DiceViewController

	controller.firstValue = randomDiceValue()
	controller.secondValue = randomDiceValue()

	present(controller, animated: true, completion: nil)
}