everybody loves afnetworking ... and so can you!

32
Everybody Loves AFNetworking ...and So Can You! Mattt Thompson Heroku

Upload: jeffsoto

Post on 06-May-2015

7.246 views

Category:

Documents


2 download

DESCRIPTION

Brooklyn iOS Developer Meetup Presenter: Mattt Thompson Topic: Everybody loves AFNetworking Location: NYU Poly Incubator DUMBO Brooklyn, NY http://www.meetup.com/The-Brooklyn-iPhone-and-iPad-Developer-Meetup/

TRANSCRIPT

Page 1: Everybody Loves AFNetworking ... and So Can you!

Everybody Loves AFNetworking

...and So Can You!

Mattt ThompsonHeroku

Page 2: Everybody Loves AFNetworking ... and So Can you!

@mattt

Page 3: Everybody Loves AFNetworking ... and So Can you!
Page 4: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

Page 5: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

Page 6: Everybody Loves AFNetworking ... and So Can you!

NSURLConnection+

NSOperation

Page 7: Everybody Loves AFNetworking ... and So Can you!

NSURLConnection

• High-Level Networking API

• Delegate-based Callbacks

Page 8: Everybody Loves AFNetworking ... and So Can you!

URL Loading System

• URL Loading

• NSURLConnection

• NSURLRequest

• NSURLResponse

• Caching

• NSURLCache

• NSURLCacheResponse

• Authentication & Credentials• NSURLCredential

• NSURLAuthenticationChallenge

• Cookies• NSHTTPCookie

• Protocols• NSProtocol

Page 9: Everybody Loves AFNetworking ... and So Can you!

NSURLConnection Delegate Methods

- connection:didReceiveResponse:

- connection:didReceiveData:

- connectionDidFinishLoading:

- connection:didFailWithError:

- connection:willCacheResponse:

Page 10: Everybody Loves AFNetworking ... and So Can you!

NSOperation

• Atomic Unit of Computation

• Concurrently executed in NSOperationQueue

• Encapsulates State

• started, executing, finished

• Cancelable

• Completion Blocks

Page 11: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

• NSOperation Subclass

• Implements NSURLConnection Delegate Methods

• Supports Streaming Uploads / Downloads

• Stores Request, Response, Data

Page 12: Everybody Loves AFNetworking ... and So Can you!

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/ip"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];

operation.completionBlock = ^ { NSLog(@"Complete: %@", operation.responseString);};

[operation start];

Page 13: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

Page 14: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

Page 15: Everybody Loves AFNetworking ... and So Can you!

AFHTTPRequestOperation

• AFURLConnectionOperation Subclass

• Adds Knowledge Specific to HTTP

• Status Codes

• MIME Types

• Adds Success / Failure Distinction

Page 16: Everybody Loves AFNetworking ... and So Can you!

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/robots.txt"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", operation.responseString);} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure: %@", error);}];

[operation start];

Page 17: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

Page 18: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

Page 19: Everybody Loves AFNetworking ... and So Can you!

Operations should encapsulate everything it

takes to get what you want

Page 20: Everybody Loves AFNetworking ... and So Can you!

Success / Failure

JSON XML Image

Status Code

Content Type

2XX 2XX 2XX

application/jsontext/json

text/javascriptapplication/xml

text/xml

image/tiffimage/jpegimage/gifimage/png

Page 21: Everybody Loves AFNetworking ... and So Can you!

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Success :%@", JSON); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Failure: %@", error); }];

[operation start];

Page 22: Everybody Loves AFNetworking ... and So Can you!

NSURL *URL = [NSURL URLWithString:@"http://example.com/avatar.jpg"];

[cell.imageView setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"placeholder"]];

Page 23: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

Page 24: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

Page 25: Everybody Loves AFNetworking ... and So Can you!

AFHTTPClient

• Designed to Work for Single Endpoint

• Set Default Headers

• Authorization, Accept, Accept-Language, etc.

• Encode Parameters to Query String or Message Body

• Handle Multipart Form Request Body Construction

• Manage Request Operations

Page 26: Everybody Loves AFNetworking ... and So Can you!

•Create NSURLRequest

•Create AFHTTPRequestOperation

•Enqueue Operations

Page 27: Everybody Loves AFNetworking ... and So Can you!

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

Page 28: Everybody Loves AFNetworking ... and So Can you!

UIKit Extensions

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist ImageA

FH

TT

PC

lien

t

OA

uth

Collection+JSON

S3

Page 29: Everybody Loves AFNetworking ... and So Can you!

AFNetworking Ecosystem

• AFOAuth1Client & AFOAuth2Client

• AFAmazonS3Client

• AFDownloadRequestOperation

• AFIncrementalStore

• AFKissXMLRequestOperation

• AFCollectionJSONRequestOperation

• AFHTTPRequestOperationLogger

Page 30: Everybody Loves AFNetworking ... and So Can you!

AFFuture

• Working Towards 1.0

• AFIncrementalStore

• More Examples & Documentation

• Modular CocoaPods Specification

Page 31: Everybody Loves AFNetworking ... and So Can you!

How You Can Help

• Documentation & Guides

• Especially non-English

• Pitch In on Stack Overflow

• Issues

• Pull Requests

Page 32: Everybody Loves AFNetworking ... and So Can you!

Thanks!