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

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.

Mattt Thompson

August 01, 2013
Tweet

More Decks by Mattt Thompson

Other Decks in Programming

Transcript

  1. 2

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

    1,300+ Closed Issues • 130 Contributors
  3. Official Extensions • OAuth 1.0a • OAuth 2.0 • Amazon

    S3 • JSON-RPC • Collection+JSON • Logger
  4. Third-Party Extensions • XAuth • CSV • iCalendar • HAR

    (HTTP Archive) • RSS • Gravatar • WebDav
  5. These are confidential sessions—please refrain from streaming, blogging, or taking

    pictures Session 705 What’s New in Foundation Networking
  6. NSURLSession • Session Configuration • URL Cache • Protocols •

    Cookie Storage • Tasks • Backgroundable • Data, Upload, & Download
  7. AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+

    • Xcode 5 • NSURLSession & NSURLConnection • Serialization Modules • UIKit Extensions • Real-time
  8. - (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 }
  9. - (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]; }]; }
  10. • Shared Between Request Operations & Session Tasks • Significantly

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

    HTTP • JSON • XML Parser • XML Document (OSX) • Property List • Image Request Response
  12. Changes to AFHTTPRequestOperation • You'll Actually Use it Directly! •

    @property responseSerializer • @property completionQueue • @property completionGroup
  13. 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];
  14. AFURLSessionManager • Provides Block-based Callbacks for Delegate Methods • Including

    Default Expected Implementation • Provides Per-Task Upload / Download Progress Callbacks • Convenience Methods for Session Management
  15. AFHTTPClient • Create Requests • Parameter Query String Serialization •

    Multipart Form Stream Creation • Default Headers • Create and Manage Request Operations • Create Batched Operations • Monitor Network Reachability
  16. New Architecture NSURLConnection NSURLSession AFHTTPRequestOperation AFURLConnectionOperation Data Upload Download NSURLSessionTask

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

    identical between Session & Request Operation Managers • Default to JSON Response Serializer • No Longer Requires baseURL
  18. - (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;
  19. - (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;
  20. - (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;
  21. - (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;
  22. - (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;
  23. UIKit Extensions • Network Activity Indicator • UIImageView • UIButton

    • UIActivityIndicatorView • UIProgressView • UIWebView
  24. R O C K E T a hybrid approach to

    real-time cloud applications
  25. 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": {...} }]
  26. 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];
  27. Helios Services • Data Synchronization • Push Notification Registration •

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

    "Model.xcdatamodel" service :push_notification service :in_app_purchase service :passbook } run app
  29. AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+

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