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.

D29bb4d2d2f2ba2c2fb5a329e1e4651f?s=128

Mattt Thompson

August 10, 2012
Tweet

Transcript

  1. Everybody Loves AFNetworking ...and So Can You! Mattt Thompson CocoaConf

    Columbus 2012
  2. None
  3. None
  4. None
  5. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient

  6. AFURLConnectionOperation

  7. NSURLConnection + NSOperation

  8. None
  9. None
  10. None
  11. NSURLConnection • High-Level Networking API • Delegate-based Callbacks

  12. URL Loading System

  13. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

    • NSURLResponse
  14. URL Loading System • URL Loading • NSURLConnection • NSURLRequest

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

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

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

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

    • NSURLResponse • Caching • NSURLCache • NSURLCacheResponse • Authentication & Credentials • NSURLCredential • NSURLAuthenticationChallenge • Cookies • NSHTTPCookie • Protocols • NSProtocol
  19. NSURLConnection Delegate Methods

  20. NSURLConnection Delegate Methods - connection:didReceiveResponse:

  21. NSURLConnection Delegate Methods - connection:didReceiveResponse: - connection:didReceiveData:

  22. NSURLConnection Delegate Methods - connection:didReceiveResponse: - connection:didReceiveData: - connectionDidFinishLoading:

  23. NSURLConnection Delegate Methods - connection:didReceiveResponse: - connection:didReceiveData: - connectionDidFinishLoading: -

    connection:didFailWithError:
  24. NSURLConnection Delegate Methods - connection:didReceiveResponse: - connection:didReceiveData: - connectionDidFinishLoading: -

    connection:didFailWithError: - connection:willCacheResponse:
  25. NSOperation • Atomic Unit of Computation • Concurrently executed in

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

    Supports Streaming Uploads / Downloads • Stores Request, Response, Data
  27. None
  28. NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/ip"];

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

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

    AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
  31. 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); };
  32. 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];
  33. AFURLConnectionOperation

  34. AFURLConnectionOperation AFHTTPRequestOperation

  35. AFHTTPRequestOperation • AFURLConnectionOperation Subclass • Adds Knowledge Specific to HTTP

    • Status Codes • MIME Types • Adds Success / Failure Distinction
  36. None
  37. NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/robots.txt"];

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

  39. 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) {
  40. 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); }];
  41. 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];
  42. AFURLConnectionOperation AFHTTPRequestOperation

  43. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image

  44. Operations should encapsulate everything it takes to get what you

    want
  45. 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
  46. None
  47. NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];

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

  49. 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) {
  50. 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);
  51. 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) {
  52. 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); }];
  53. 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];
  54. None
  55. NSURL *URL = [NSURL URLWithString:@"http://example.com/avatar.jpg"];

  56. NSURL *URL = [NSURL URLWithString:@"http://example.com/avatar.jpg"]; [cell.imageView setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"placeholder"]];

  57. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image

  58. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient

  59. 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
  60. •Create NSURLRequest •Create AFHTTPRequestOperation •Enqueue Operations

  61. Extremely Subclassable

  62. None
  63. •Create NSURLRequest -requestWithMethod:path:parameters

  64. •Create NSURLRequest -requestWithMethod:path:parameters •Create AFHTTPRequestOperation -HTTPRequestOperationWithRequest: success:failure

  65. •Create NSURLRequest -requestWithMethod:path:parameters •Create AFHTTPRequestOperation -HTTPRequestOperationWithRequest: success:failure •Enqueue Operations -enqueueHTTPRequestOperation:

  66. Shared Singleton Pattern

  67. + (AFTwitterAPIClient *)sharedClient { static AFTwitterAPIClient *_sharedClient = nil; static

    dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedClient = [[AFTwitterAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFTwitterAPIBaseURLString]]; }); return _sharedClient; }
  68. NSString *path = @"statuses/public_timeline.json"; [[AFTwitterAPIClient sharedClient] getPath:path parameters:nil success:... failure:...];

  69. Demo

  70. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient

  71. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient

  72. AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient Collection +JSON

  73. UIKit Extensions AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient Collection

    +JSON
  74. UIKit Extensions AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient OAuth

    Collection +JSON
  75. UIKit Extensions AFURLConnectionOperation AFHTTPRequestOperation JSON XML plist Image AFHTTPClient OAuth

    Collection +JSON S3
  76. AFNetworking Ecosystem • AFOAuth1Client & AFOAuth2Client • AFAmazonS3Client • AFDownloadRequestOperation

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

    & Documentation • Modular CocoaPods Specification
  78. AFIncrementalStore The “Holy Grail” of Networked Apps

  79. Core Data

  80. + Networking Core Data

  81. Core Data

  82. None
  83. NSManagedObjectContext

  84. NSPersistentStoreCoordinator NSManagedObjectContext

  85. NSPersistentStoreCoordinator NSManagedObjectContext NSPersistentStore

  86. NSPersistentStoreCoordinator NSManagedObjectContext NSSQLiteStore AFIncrementalStore

  87. •NSFetchRequest •Attribute Faults •Relationship Faults

  88. Rack::CoreData Generate REST API Scaffold from Core Data

  89. None
  90. GET /companies POST /suppliers PUT /customers/:id GET /country/:id/companies

  91. Demo

  92. How You Can Help • Documentation & Guides • Especially

    non-English • Pitch In on Stack Overflow • Issues • Pull Requests
  93. @mattt

  94. What I Learned From AFNetworking's GitHub Issues Mattt Thompson CocoaConf

    Columbus 2012
  95. Thanks!