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!

Everybody Loves AFNetworking ... and So Can you!

  • 1.
    Everybody Loves AFNetworking ...and So Can You! Mattt Thompson Heroku
  • 2.
  • 4.
    AFHTTPClient JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 5.
  • 6.
    NSURLConnection + NSOperation
  • 7.
    NSURLConnection • High-Level NetworkingAPI • 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 Unitof 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];
  • 13.
  • 14.
  • 15.
    AFHTTPRequestOperation • AFURLConnectionOperationSubclass • 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];
  • 17.
  • 18.
    JSON XML plist Image AFHTTPRequestOperation AFURLConnectionOperation
  • 19.
    Operations should encapsulateeverything 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 toWork 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 Towards1.0 • AFIncrementalStore • More Examples & Documentation • Modular CocoaPods Specification
  • 31.
    How You CanHelp • Documentation & Guides • Especially non-English • Pitch In on Stack Overflow • Issues • Pull Requests
  • 32.