AFNetworking 2.0

AFNetworking 2.0

Video: https://vimeo.com/71689429

Announcing AFNetworking 2.0 to the New York iOS Developer Meetup on August 1, 2013, and CocoaConf PDX on August 16, 2013.

D29bb4d2d2f2ba2c2fb5a329e1e4651f?s=128

Mattt Thompson

August 01, 2013
Tweet

Transcript

  1. 2

  2. None
  3. None
  4. None
  5. @mattt @sco

  6. None
  7. None
  8. None
  9. None
  10. State of the Library

  11. • 8,800+ Stars • 2,200+ Forks • 1,500+ Commits •

    1,300+ Closed Issues • 130 Contributors
  12. Continuous Integration Automated Documentation

  13. Official Extensions • OAuth 1.0a • OAuth 2.0 • Amazon

    S3 • JSON-RPC • Collection+JSON • Logger
  14. Official Extensions

  15. Official Extensions http://mneorr.github.io/Alcatraz/ Download Xcode Template with Alcatraz

  16. Third-Party Extensions • XAuth • CSV • iCalendar • HAR

    (HTTP Archive) • RSS • Gravatar • WebDav
  17. Built on AFNetworking RestKit NimbusKit OctoKit

  18. 10,000's Built on AFNetworking Apps Built Using AFNetworking

  19. None
  20. None
  21. These are confidential sessions—please refrain from streaming, blogging, or taking

    pictures Session 705 What’s New in Foundation Networking
  22. NSURLSession

  23. NSURLSession • Session Configuration • URL Cache • Protocols •

    Cookie Storage • Tasks • Backgroundable • Data, Upload, & Download
  24. None
  25. AFNetworking 2.0

  26. None
  27. AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+

    • Xcode 5 • NSURLSession & NSURLConnection • Serialization Modules • UIKit Extensions • Real-time
  28. Requirements

  29. Serializers

  30. AFURLConnectionOperation AFHTTPRequestOperation AFJSONRequestOperation

  31. - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError

    *error))failure { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-retain-cycles" #pragma clang diagnostic ignored "-Wgnu" self.completionBlock = ^ { if (self.error) { if (failure) { dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ failure(self, self.error); }); } } else { dispatch_async(json_request_operation_processing_queue(), ^{ id JSON = self.responseJSON; if (self.error) { if (failure) { dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ failure(self, self.error); }); } } else { if (success) { dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ success(self, JSON); }); } } }); } }; #pragma clang diagnostic pop }
  32. - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError

    *error))failure { __weak __typeof(self)weakSelf = self; [super setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { __strong __typeof(weakSelf)strongSelf = weakSelf; if (![responseObject isKindOfClass:[NSData class]]) { [strongSelf setResponseJSON:responseObject]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { __strong __typeof(weakSelf)strongSelf = weakSelf; [strongSelf setError:error]; }]; }
  33. op.responseSerializer = [AFJSONSerializer serializer];

  34. <AFURLRequestSerialization> <AFURLResponseSerialization> - requestBySerializingRequest: withParameters: error: - responseObjectForResponse: data: error:

  35. • Shared Between Request Operations & Session Tasks • Significantly

    Reduced Boilerplate Code • Pluggable & Configurable • Protocol, Not Subclass Serializers
  36. Built-in Serializers • HTTP • JSON • Property List •

    HTTP • JSON • XML Parser • XML Document (OSX) • Property List • Image Request Response
  37. Serializer Extensions • MsgPack • CSV / TSV • vCard

    • vCal • WebP
  38. Object Serialization • NSObject • NSManagedObject • RKManagedObject • MTLModel

  39. None
  40. None
  41. Changes to AFHTTPRequestOperation • You'll Actually Use it Directly! •

    @property responseSerializer • @property completionQueue • @property completionGroup
  42. NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.responseSerializer = [AFJSONSerializer serializer]; [operation start];
  43. AFURLConnectionOperation AFHTTPRequestOperation AFJSONRequestOperation

  44. AFURLConnectionOperation AFHTTPRequestOperation

  45. Content Operation Extension • AFJSONRequestOperation • AFXMLRequestOperation • AFPropertyListRequestOperation •

    AFImageRequestOperation
  46. AFURLSessionManager

  47. NSURLSession <NSURLSessionDelegate> 2 Methods <NSURLSessionTaskDelegate> 5 Methods <NSURLSessionDataDelegate> 4 Methods

    <NSURLSessionDownloadDelegate> 3 Methods
  48. NSURLSession 14 Delegate Methods

  49. AFURLSessionManager • Provides Block-based Callbacks for Delegate Methods • Including

    Default Expected Implementation • Provides Per-Task Upload / Download Progress Callbacks • Convenience Methods for Session Management
  50. AFHTTPClient the

  51. AFHTTPClient • Create Requests • Parameter Query String Serialization •

    Multipart Form Stream Creation • Default Headers • Create and Manage Request Operations • Create Batched Operations • Monitor Network Reachability
  52. New Architecture • <AFURLRequestSerialization> • <AFURLResponseSerialization> • AFSecurityPolicy • AFNetworkReachabilityManager

  53. New Architecture NSURLConnection NSURLSession AFHTTPRequestOperation AFURLConnectionOperation Data Upload Download NSURLSessionTask

    NSURLSessionTask NSURLSessionTask AFHTTPClient AFHTTPSessionManager AFURLSessionManager
  54. AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost

    identical between Session & Request Operation Managers • Default to JSON Response Serializer • No Longer Requires baseURL
  55. - (Object *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *, id))success

    failure:(void (^)(Object *, NSError *))failure; - (Object *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *))success failure:(void (^)(Object *, NSError *))failure;
  56. - (Object *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *, id))success

    failure:(void (^)(Object *, NSError *))failure; - (Object *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData>))block success:(void (^)(Object *, id))success failure:(void (^)(Object *, NSError *))failure;
  57. - (Object *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *, id))success

    failure:(void (^)(Object *, NSError *))failure; - (Object *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *, id))success failure:(void (^)(Object *, NSError *))failure; - (Object *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(Object *))success failure:(void (^)(Object *, NSError *))failure;
  58. - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)( NSHTTPURLResponse *, id, NSError

    *error ))completionHandler;
  59. - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL progress:(NSProgress **)progress completionHandler:(void (^)(

    NSHTTPURLResponse *, id, NSError *error ))completionHandler; - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData progress:(NSProgress **)progress completionHandler:(void (^)( NSHTTPURLResponse *, id, NSError *error ))completionHandler;
  60. - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request progress:(NSProgress **)progress completionHandler:(void (^)( NSHTTPURLResponse *,

    NSURL *, NSError *error ))completionHandler; - (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData progress:(NSProgress **)progress completionHandler:(void (^)( NSHTTPURLResponse *, NSURL *, NSError *error ))completionHandler;
  61. AFSecurityPolicy • Evaluate Server Trust • Pinned Certificates • Pinned

    Public Keys • Validate Server Certs
  62. AFNetworkReachability Manager • Monitor Network Reachability • Domain • IP

    Address • Determine Type of Connection
  63. UIKit Extensions

  64. UIKit Extensions • Network Activity Indicator • UIImageView • UIButton

    • UIActivityIndicatorView • UIProgressView • UIWebView
  65. Real-time

  66. R O C K E T a hybrid approach to

    real-time cloud applications
  67. Server-Sent Events W3C Event Source Draft Spec JSON Patch RFC

    6902
  68. Document Stream Request Response GET /resources Accept: application/json SUBSCRIBE /resources

    Accept: text/event-stream HTTP/1.1 200 OK Content-Type: application/json {"resources": [...]} HTTP/1.1 200 OK Content-Type: text/event-stream event: patch data: [{ "op": "add", "path": "/resources/123", "value": {...} }]
  69. None
  70. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager SUBSCRIBE:@"http://example.com/resources" usingBlock:^(NSArray *operations, NSError

    *error) { for (AFJSONPatchOperation *operation in operations) { switch (operation.type) { case AFJSONAddOperationType: [self.resources addObject:operation.value]; break; case AFJSONRemoveOperationType: [self.resources removeObject:operation.value]; break; } } } error:nil]; [manager GET:@"http://example.com/resources" parameters:nil success:^(NSHTTPURLResponse *response, id responseObject) { self.resources = responseObject; } failure:nil];
  71. rocket.github.io

  72. helios an extensible open source mobile backend framework

  73. Helios Services • Data Synchronization • Push Notification Registration •

    In-App Purchase • Passbook Webservice • Newsstand Syndication • Logging & Analytics
  74. require 'bundler' Bundler.require app = Helios::Application.new { service :data, model:

    "Model.xcdatamodel" service :push_notification service :in_app_purchase service :passbook } run app
  75. helios.io

  76. AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+

    • Xcode 5 • NSURLSession & NSURLConnection • Serialization Modules • UIKit Extensions • Real-time
  77. AFNetworking 2.0 GA Soon!

  78. None
  79. None
  80. @mattt

  81. One more thing...

  82. AFNetworking the definitive guide

  83. AFNetworking the definitive guide Coming Fall 2013

  84. Thank !