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

Building Next-Generation iOS Apps with AFNetwor...

Kod.io
October 26, 2013

Building Next-Generation iOS Apps with AFNetworking 2.0

By Mattt Thompson
http://kod.io

Kod.io

October 26, 2013
Tweet

More Decks by Kod.io

Other Decks in Programming

Transcript

  1. 2

  2. • 9,200+ Stars • 2,400+ Forks • 1,500+ Commits •

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

    S3 • JSON-RPC • Collection+JSON • Logger
  4. • XAuth • CSV • iCalendar • HAR (HTTP Archive)

    • RSS • Gravatar • WebDav Third-Party Extensions
  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. • Shared Between Request Operations & Session Tasks • Significantly

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

    HTTP • JSON • XML Parser • XML Document (OSX) • Property List • Image Request Response
  11. 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];
  12. AFURLSessionManager • Provides Block-based Callbacks for Delegate Methods • Including

    Default Expected Implementation • Provides Per-Task Upload / Download Progress Callbacks • Convenience Methods for Session Management
  13. - (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;
  14. - (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;
  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. AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost

    identical between Session & Request Operation Managers
  17. AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost

    identical between Session & Request Operation Managers • Default to JSON Response Serializer
  18. AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost

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

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

    real-time cloud applications
  24. Request Response Document GET /resources Accept: application/json HTTP/1.1 200 OK

    Content-Type: application/json ! {"resources": [...]}
  25. Request Response Document GET /resources Accept: application/json HTTP/1.1 200 OK

    Content-Type: application/json ! {"resources": [...]} Stream SUBSCRIBE /resources Accept: text/event-stream 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/setup'! ! 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
  30. NS

  31. 2