Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Everybody Loves AFNetworking And So Can You!

Everybody Loves AFNetworking And So Can You!

(Presented at CocoaConf Columbus 2012)

Learn how to get started with AFNetworking, and how to make the most of it in your iOS or Mac app. Request JSON effortlessly! Consume REST APIs like a champ! Download and cache images blindfolded-with-one-hand-tied-behind-your-back! We'll even touch on some of the best parts of Foundation and the Objective-C language itself, from NSOperation and the URL Loading System to blocks and Grand Central Dispatch.

Mattt Thompson

August 10, 2012
Tweet

More Decks by Mattt Thompson

Other Decks in Programming

Transcript

  1. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse
  2. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse
  3. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse • Authentication & Credentials • NSURLCredential • NSURLAuthenticationChallenge
  4. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse • Authentication & Credentials • NSURLCredential • NSURLAuthenticationChallenge • Cookies • NSHTTPCookie
  5. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse • Authentication & Credentials • NSURLCredential • NSURLAuthenticationChallenge • Cookies • NSHTTPCookie • Protocols • NSProtocol
  6. NSOperation • Atomic Unit of Computation • Concurrently executed in

    NSOperationQueue • Encapsulates State • started, executing, finished • Cancelable • Completion Blocks
  7. AFURLConnectionOperation • NSOperation Subclass • Implements NSURLConnection Delegate Methods •

    Supports Streaming Uploads / Downloads • Stores Request, Response, Data
  8. NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/ip"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
  9. 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); };
  10. 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];
  11. AFHTTPRequestOperation • AFURLConnectionOperation Subclass • Adds Knowledge Specific to HTTP

    • Status Codes • MIME Types • Adds Success / Failure Distinction
  12. 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) {
  13. 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); }];
  14. 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];
  15. Success / Failure JSON XML Image Status Code Content Type

    2XX 2XX 2XX application/json text/json text/javascript application/xml text/xml image/tiff image/jpeg image/gif image/png
  16. 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) {
  17. 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);
  18. 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) {
  19. 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); }];
  20. 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];
  21. 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 • Enqueue Operations in Batches
  22. + (AFTwitterAPIClient *)sharedClient { static AFTwitterAPIClient *_sharedClient = nil; static

    dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedClient = [[AFTwitterAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFTwitterAPIBaseURLString]]; }); return _sharedClient; }
  23. AFNetworking Ecosystem • AFOAuth1Client & AFOAuth2Client • AFAmazonS3Client • AFDownloadRequestOperation

    • AFIncrementalStore • AFKissXMLRequestOperation • AFCollectionJSONRequestOperation • AFHTTPRequestOperationLogger
  24. AFFuture • Working Towards 1.0 • AFIncrementalStore • More Examples

    & Documentation • Modular CocoaPods Specification
  25. How You Can Help • Documentation & Guides • Especially

    non-English • Pitch In on Stack Overflow • Issues • Pull Requests