Everybody Loves AFNetworking ... and So Can you!

Post on 06-May-2015

7,243 views 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

Everybody Loves AFNetworking

...and So Can You!

Mattt ThompsonHeroku

@mattt

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

AFURLConnectionOperation

NSURLConnection+

NSOperation

NSURLConnection

• High-Level Networking API

• Delegate-based Callbacks

URL Loading System

• URL Loading

• NSURLConnection

• NSURLRequest

• NSURLResponse

• Caching

• NSURLCache

• NSURLCacheResponse

• Authentication & Credentials• NSURLCredential

• NSURLAuthenticationChallenge

• Cookies• NSHTTPCookie

• Protocols• NSProtocol

NSURLConnection Delegate Methods

- connection:didReceiveResponse:

- connection:didReceiveData:

- connectionDidFinishLoading:

- connection:didFailWithError:

- connection:willCacheResponse:

NSOperation

• Atomic Unit of Computation

• Concurrently executed in NSOperationQueue

• Encapsulates State

• started, executing, finished

• Cancelable

• Completion Blocks

AFURLConnectionOperation

• NSOperation Subclass

• Implements NSURLConnection Delegate Methods

• Supports Streaming Uploads / Downloads

• Stores Request, Response, Data

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];

AFURLConnectionOperation

AFURLConnectionOperation

AFHTTPRequestOperation

AFHTTPRequestOperation

• AFURLConnectionOperation Subclass

• Adds Knowledge Specific to HTTP

• Status Codes

• MIME Types

• Adds Success / Failure Distinction

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];

AFURLConnectionOperation

AFHTTPRequestOperation

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

Operations should encapsulate everything it

takes to get what you want

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

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];

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

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

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

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

•Create NSURLRequest

•Create AFHTTPRequestOperation

•Enqueue Operations

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist Image

AF

HT

TP

Clie

nt

UIKit Extensions

AFURLConnectionOperation

AFHTTPRequestOperation

JSON XML plist ImageA

FH

TT

PC

lien

t

OA

uth

Collection+JSON

S3

AFNetworking Ecosystem

• AFOAuth1Client & AFOAuth2Client

• AFAmazonS3Client

• AFDownloadRequestOperation

• AFIncrementalStore

• AFKissXMLRequestOperation

• AFCollectionJSONRequestOperation

• AFHTTPRequestOperationLogger

AFFuture

• Working Towards 1.0

• AFIncrementalStore

• More Examples & Documentation

• Modular CocoaPods Specification

How You Can Help

• Documentation & Guides

• Especially non-English

• Pitch In on Stack Overflow

• Issues

• Pull Requests

Thanks!