SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Everybody Loves
    AFNetworking
 ...and So Can You!
Mattt Thompson
Heroku
@mattt
AFHTTPClient
JSON    XML     plist   Image



   AFHTTPRequestOperation



  AFURLConnectionOperation
AFURLConnectionOperation
NSURLConnection
      +
  NSOperation
NSURLConnection

• High-Level Networking API
• Delegate-based Callbacks
URL Loading System

• URL Loading                • Authentication &
                               Credentials
    •   NSURLConnection
    •   NSURLRequest           •   NSURLCredential

    •   NSURLResponse          •   NSURLAuthenticationChallenge



•   Caching                  • Cookies
    •   NSURLCache             •   NSHTTPCookie

    •   NSURLCacheResponse
                             • 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
AFHTTPRequestOperation



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



AFURLConnectionOperation
JSON    XML     plist   Image



   AFHTTPRequestOperation



  AFURLConnectionOperation
Operations should
 encapsulate everything it
takes to get what you want
Success / Failure

              JSON               XML           Image


Status          2XX               2XX             2XX
Code

                                               image/tiff
Content   application/json
             text/json
                             application/xml   image/jpeg
 Type     text/javascript
                                text/xml       image/gif
                                               image/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"]];
JSON    XML     plist   Image



   AFHTTPRequestOperation



  AFURLConnectionOperation
AFHTTPClient
JSON    XML     plist   Image



   AFHTTPRequestOperation



  AFURLConnectionOperation
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
AFHTTPClient
JSON    XML     plist   Image



   AFHTTPRequestOperation



  AFURLConnectionOperation
Collection                    UIKit
 +JSON                     Extensions




                                                       OAuth
                                        AFHTTPClient
 JSON        XML   plist    Image




                                                       S3
      AFHTTPRequestOperation



     AFURLConnectionOperation
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!

Mais conteúdo relacionado

Mais procurados

Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4DEVCON
 
MySQL in Go - Golang NE July 2015
MySQL in Go - Golang NE July 2015MySQL in Go - Golang NE July 2015
MySQL in Go - Golang NE July 2015Mark Hemmings
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Libraryasync_io
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to CeleryIdan Gazit
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryMauro Rocco
 
Talk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe ConversetTalk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe ConversetCocoaHeads France
 
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Satoshi Asano
 
Callbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptCallbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptŁukasz Kużyński
 
HTML5 JavaScript APIs
HTML5 JavaScript APIsHTML5 JavaScript APIs
HTML5 JavaScript APIsRemy Sharp
 
Practical JavaScript Promises
Practical JavaScript PromisesPractical JavaScript Promises
Practical JavaScript PromisesAsa Kusuma
 
The Promised Land (in Angular)
The Promised Land (in Angular)The Promised Land (in Angular)
The Promised Land (in Angular)Domenic Denicola
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.jsMatthew Beale
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jscacois
 
Tools for Solving Performance Issues
Tools for Solving Performance IssuesTools for Solving Performance Issues
Tools for Solving Performance IssuesOdoo
 
JavaScript Sprachraum
JavaScript SprachraumJavaScript Sprachraum
JavaScript Sprachraumpatricklee
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript FrameworkAll Things Open
 
Java Play RESTful ebean
Java Play RESTful ebeanJava Play RESTful ebean
Java Play RESTful ebeanFaren faren
 

Mais procurados (20)

Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4
 
MySQL in Go - Golang NE July 2015
MySQL in Go - Golang NE July 2015MySQL in Go - Golang NE July 2015
MySQL in Go - Golang NE July 2015
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to Celery
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & Celery
 
Talk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe ConversetTalk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe Converset
 
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Webエンジニアから見たiOS5
Webエンジニアから見たiOS5
 
Callbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptCallbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascript
 
Practical Celery
Practical CeleryPractical Celery
Practical Celery
 
HTML5 JavaScript APIs
HTML5 JavaScript APIsHTML5 JavaScript APIs
HTML5 JavaScript APIs
 
Practical JavaScript Promises
Practical JavaScript PromisesPractical JavaScript Promises
Practical JavaScript Promises
 
The Promised Land (in Angular)
The Promised Land (in Angular)The Promised Land (in Angular)
The Promised Land (in Angular)
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.js
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.js
 
Tools for Solving Performance Issues
Tools for Solving Performance IssuesTools for Solving Performance Issues
Tools for Solving Performance Issues
 
JavaScript Sprachraum
JavaScript SprachraumJavaScript Sprachraum
JavaScript Sprachraum
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
 
Java Play RESTful ebean
Java Play RESTful ebeanJava Play RESTful ebean
Java Play RESTful ebean
 
AJAX Transport Layer
AJAX Transport LayerAJAX Transport Layer
AJAX Transport Layer
 
Protractor Training in Pune by QuickITDotnet
Protractor Training in Pune by QuickITDotnet Protractor Training in Pune by QuickITDotnet
Protractor Training in Pune by QuickITDotnet
 

Semelhante a Everybody Loves AFNetworking and So Can You

Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRichard Lee
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingIstanbul Tech Talks
 
Attack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaAttack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaPrajal Kulkarni
 
mobile in the cloud with diamonds. improved.
mobile in the cloud with diamonds. improved.mobile in the cloud with diamonds. improved.
mobile in the cloud with diamonds. improved.Oleg Shanyuk
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
RESTful modules in zf2
RESTful modules in zf2RESTful modules in zf2
RESTful modules in zf2Corley S.r.l.
 
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfO365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfNCCOMMS
 
ERRest - Designing a good REST service
ERRest - Designing a good REST serviceERRest - Designing a good REST service
ERRest - Designing a good REST serviceWO Community
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr DevelopersErik Hatcher
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjobMark Greenway
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjobMark Greenway
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupErnest Jumbe
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHPZoran Jeremic
 

Semelhante a Everybody Loves AFNetworking and So Can You (20)

ERRest and Dojo
ERRest and DojoERRest and Dojo
ERRest and Dojo
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
 
Attack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaAttack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and Kibana
 
mobile in the cloud with diamonds. improved.
mobile in the cloud with diamonds. improved.mobile in the cloud with diamonds. improved.
mobile in the cloud with diamonds. improved.
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
AJAX
AJAXAJAX
AJAX
 
Ajax
AjaxAjax
Ajax
 
RESTful modules in zf2
RESTful modules in zf2RESTful modules in zf2
RESTful modules in zf2
 
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfO365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
 
ERRest - Designing a good REST service
ERRest - Designing a good REST serviceERRest - Designing a good REST service
ERRest - Designing a good REST service
 
[Struyf] Automate Your Tasks With Azure Functions
[Struyf] Automate Your Tasks With Azure Functions[Struyf] Automate Your Tasks With Azure Functions
[Struyf] Automate Your Tasks With Azure Functions
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr Developers
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjob
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjob
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 
URLProtocol
URLProtocolURLProtocol
URLProtocol
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup Group
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHP
 

Everybody Loves AFNetworking and So Can You

  • 1. Everybody Loves AFNetworking ...and So Can You! Mattt Thompson Heroku
  • 3.
  • 4. AFHTTPClient JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 6. NSURLConnection + NSOperation
  • 7. NSURLConnection • High-Level Networking API • Delegate-based Callbacks
  • 8. URL Loading System • URL Loading • Authentication & Credentials • NSURLConnection • NSURLRequest • NSURLCredential • NSURLResponse • NSURLAuthenticationChallenge • Caching • Cookies • NSURLCache • NSHTTPCookie • NSURLCacheResponse • Protocols • NSProtocol
  • 9. NSURLConnection Delegate Methods - connection:didReceiveResponse: - connection:didReceiveData: - connectionDidFinishLoading: - connection:didFailWithError: - connection:willCacheResponse:
  • 10. NSOperation • Atomic Unit of Computation • Concurrently executed in NSOperationQueue • Encapsulates State • started, executing, finished • Cancelable • Completion Blocks
  • 11. AFURLConnectionOperation • NSOperation Subclass • Implements NSURLConnection Delegate Methods • Supports Streaming Uploads / Downloads • Stores Request, Response, Data
  • 12. 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];
  • 15. AFHTTPRequestOperation • AFURLConnectionOperation Subclass • Adds Knowledge Specific to HTTP • Status Codes • MIME Types • Adds Success / Failure Distinction
  • 16. 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];
  • 18. JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 19. Operations should encapsulate everything it takes to get what you want
  • 20. Success / Failure JSON XML Image Status 2XX 2XX 2XX Code image/tiff Content application/json text/json application/xml image/jpeg Type text/javascript text/xml image/gif image/png
  • 21. 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];
  • 22. NSURL *URL = [NSURL URLWithString:@"http://example.com/avatar.jpg"]; [cell.imageView setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"placeholder"]];
  • 23. JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 24. AFHTTPClient JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 25. 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
  • 26. • Create NSURLRequest • Create AFHTTPRequestOperation • Enqueue Operations
  • 27. AFHTTPClient JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 28. Collection UIKit +JSON Extensions OAuth AFHTTPClient JSON XML plist Image S3 AFHTTPRequestOperation AFURLConnectionOperation
  • 29. AFNetworking Ecosystem • AFOAuth1Client & AFOAuth2Client • AFAmazonS3Client • AFDownloadRequestOperation • AFIncrementalStore • AFKissXMLRequestOperation • AFCollectionJSONRequestOperation • AFHTTPRequestOperationLogger
  • 30. AFFuture • Working Towards 1.0 • AFIncrementalStore • More Examples & Documentation • Modular CocoaPods Specification
  • 31. How You Can Help • Documentation & Guides • Especially non-English • Pitch In on Stack Overflow • Issues • Pull Requests