Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building Next-Generation iOS Apps with AFNetwor...
Search
Kod.io
October 26, 2013
Programming
2
370
Building Next-Generation iOS Apps with AFNetworking 2.0
By Mattt Thompson
http://kod.io
Kod.io
October 26, 2013
Tweet
Share
More Decks by Kod.io
See All by Kod.io
Our future is in our past or whatever
kodio
0
67
You should learn Go!
kodio
1
130
Daha iyi Geliştirme Ortamı için: Vagrant
kodio
0
67
Reactive Principles
kodio
1
150
AngularJS vs. Ember.js vs. Backbone.js
kodio
1
510
iBeacons: Interacting with the physical world using Bluetooth LE
kodio
1
100
Go: a simple programming environment
kodio
2
170
Why I had to write a client side framework for Koding.
kodio
1
95
Sure, I could learn Objective-C but...
kodio
1
90
Other Decks in Programming
See All in Programming
Discover Metal 4
rei315
2
140
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
5
1.1k
AIともっと楽するE2Eテスト
myohei
6
2.7k
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
830
#QiitaBash MCPのセキュリティ
ryosukedtomita
1
1.3k
Rails Frontend Evolution: It Was a Setup All Along
skryukov
0
150
「テストは愚直&&網羅的に書くほどよい」という誤解 / Test Smarter, Not Harder
munetoshi
0
170
テストから始めるAgentic Coding 〜Claude Codeと共に行うTDD〜 / Agentic Coding starts with testing
rkaga
12
4.5k
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
320
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
530
AI コーディングエージェントの時代へ:JetBrains が描く開発の未来
masaruhr
1
160
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
150
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
72
4.9k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Adopting Sorbet at Scale
ufuk
77
9.5k
Become a Pro
speakerdeck
PRO
29
5.4k
Producing Creativity
orderedlist
PRO
346
40k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Side Projects
sachag
455
42k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Designing Experiences People Love
moore
142
24k
Transcript
2
None
None
Objective-C & Cocoa
[❤]
• Foundation • Grand Central Dispatch • Core Animation •
Core Data • Map Kit System Frameworks
TCP BSD Sockets CFNetwork Foundation AFNetworking
None
None
None
@mattt @sco
None
None
1. NSURLConnection + NSOperation
1. NSURLConnection + NSOperation 2. Block Callbacks
None
None
None
State of the Library
• 9,200+ Stars • 2,400+ Forks • 1,500+ Commits •
1,400+ Closed Issues • 147 Contributors
Continuous Integration Automated Documentation
CocoaPods
platform :ios, “7.0” ! pod “AFNetworking”, “~> 2.0”
$ pod install
Official Extensions • OAuth 1.0a • OAuth 2.0 • Amazon
S3 • JSON-RPC • Collection+JSON • Logger
• XAuth • CSV • iCalendar • HAR (HTTP Archive)
• RSS • Gravatar • WebDav Third-Party Extensions
Built on AFNetworking RestKit NimbusKit OctoKit
10,000's Built on AFNetworking Apps Built Using AFNetworking
None
None
These are confidential sessions—please refrain from streaming, blogging, or taking
pictures Session 705 What’s New in Foundation Networking
ASCIIwwdc.com
NSURLSession
NSURLSession
NSURLSession • Session Configuration • URL Cache • Protocols •
Cookie Storage
NSURLSession • Session Configuration • URL Cache • Protocols •
Cookie Storage • Tasks • Backgroundable • Data, Upload, & Download
AFNetworking 2.0
AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+
• Xcode 5 • NSURLSession & NSURLConnection • Serialization Modules • UIKit Extensions • Real-time
Requirements
Serializers
None
AFURLConnectionOperation
AFURLConnectionOperation AFHTTPRequestOperation
AFURLConnectionOperation AFHTTPRequestOperation AFJSONRequestOperation
- (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 }
op.responseSerializer = [AFJSONSerializer serializer];
<AFURLRequestSerialization> <AFURLResponseSerialization> - requestBySerializingRequest: withParameters: error: - responseObjectForResponse: data: error:
• Shared Between Request Operations & Session Tasks • Significantly
Reduced Boilerplate Code • Pluggable & Configurable • Protocol, Not Subclass Serializers
Built-in Serializers Request Response
Built-in Serializers • HTTP • JSON • Property List Request
Response
Built-in Serializers • HTTP • JSON • Property List •
HTTP • JSON • XML Parser • XML Document (OSX) • Property List • Image Request Response
Serializer Extensions • MsgPack • CSV / TSV • vCard
• vCal • WebP
Object Serialization • NSObject • NSManagedObject • MTLModel
None
None
None
None
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];
AFURLConnectionOperation AFHTTPRequestOperation AFJSONRequestOperation
AFURLConnectionOperation AFHTTPRequestOperation
AFURLSessionManager
NSURLSession
NSURLSession <NSURLSessionDelegate> 2 Methods
NSURLSession <NSURLSessionDelegate> 2 Methods <NSURLSessionTaskDelegate> 5 Methods
NSURLSession <NSURLSessionDelegate> 2 Methods <NSURLSessionTaskDelegate> 5 Methods <NSURLSessionDataDelegate> 4 Methods
NSURLSession <NSURLSessionDelegate> 2 Methods <NSURLSessionTaskDelegate> 5 Methods <NSURLSessionDataDelegate> 4 Methods
<NSURLSessionDownloadDelegate> 3 Methods
NSURLSession Delegate Methods
NSURLSession 14 Delegate Methods
AFURLSessionManager • Provides Block-based Callbacks for Delegate Methods • Including
Default Expected Implementation • Provides Per-Task Upload / Download Progress Callbacks • Convenience Methods for Session Management
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)( NSHTTPURLResponse *, id, NSError
*error ))completionHandler;
- (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;
- (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;
AFHTTPClient
AFHTTPClient the death of
AFHTTPClient • Create Requests • Parameter Query String Serialization •
Multipart Form Stream Creation • Default Headers • Create and Manage Request Operations • Create Batched Operations • Monitor Network Reachability
New Architecture
New Architecture • <AFURLRequestSerialization> • <AFURLResponseSerialization> • AFSecurityPolicy • AFNetworkReachabilityManager
New Architecture NSURLConnection NSURLSession
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation NSURLSessionTask
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation NSURLSessionTask Data Upload Download
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation NSURLSessionTask Data Upload Download
AFHTTPRequestOperationManager
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation NSURLSessionTask Data Upload Download
AFHTTPRequestOperationManager AFURLSessionManager
New Architecture NSURLConnection NSURLSession AFURLConnectionOperation AFHTTPRequestOperation NSURLSessionTask Data Upload Download
AFHTTPRequestOperationManager AFURLSessionManager AFHTTPSessionManager
AFHTTPSessionManager
AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods
AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost
identical between Session & Request Operation Managers
AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost
identical between Session & Request Operation Managers • Default to JSON Response Serializer
AFHTTPSessionManager • Expanded & Refactored HTTP Convenience Methods • Almost
identical between Session & Request Operation Managers • Default to JSON Response Serializer • No Longer Requires baseURL
- (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;
- (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;
- (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;
AFSecurityPolicy
AFSecurityPolicy • Evaluate Server Trust • Pinned Certificates • Pinned
Public Keys
AFSecurityPolicy • Evaluate Server Trust • Pinned Certificates • Pinned
Public Keys • Validate Server Certs
AFNetworkReachability Manager
AFNetworkReachability Manager • Monitor Network Reachability • Domain • IP
Address
AFNetworkReachability Manager • Monitor Network Reachability • Domain • IP
Address • Determine Type of Connection
UIKit Extensions
UIKit Extensions
UIKit Extensions • Network Activity Indicator
UIKit Extensions • Network Activity Indicator • UIImageView
UIKit Extensions • Network Activity Indicator • UIImageView • UIButton
UIKit Extensions • Network Activity Indicator • UIImageView • UIButton
• UIActivityIndicatorView
UIKit Extensions • Network Activity Indicator • UIImageView • UIButton
• UIActivityIndicatorView • UIProgressView
UIKit Extensions • Network Activity Indicator • UIImageView • UIButton
• UIActivityIndicatorView • UIProgressView • UIWebView
Real-time
R O C K E T a hybrid approach to
real-time cloud applications
Server-Sent Events W3C Event Source Draft Spec JSON Patch RFC
6902
Request Response
Request Response Document GET /resources Accept: application/json HTTP/1.1 200 OK
Content-Type: application/json ! {"resources": [...]}
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": {...} }]
None
None
None
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:@"http://example.com/resources" parameters:nil success:^(NSHTTPURLResponse *response,
id responseObject) { self.resources = responseObject; } failure:nil];
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];
rocket.github.io
helios an extensible open source mobile backend framework
Helios Services • Data Synchronization • Push Notification Registration •
In-App Purchase • Passbook Webservice • Newsstand Syndication • Logging & Analytics
require 'bundler/setup'! ! app = Helios::Application.new {! service :data, model:
"Model.xcdatamodel" service :push_notification! service :in_app_purchase! service :passbook! }! ! run app!
$ helios link
None
helios.io
None
None
None
AFNetworking 2.0 • iOS 6+ & Mac OS X 10.8+
• Xcode 5 • NSURLSession & NSURLConnection • Serialization Modules • UIKit Extensions • Real-time
github.com/AFNetworking/AFNetworking/wiki
nshipster.com/AFNetworking-2
NS
• http://afnetworking.com • http://cocoapods.org • http://asciiwwdc.com • http://helios.io • http://rocket.github.io
• http://nshipster.com • http://objc.io @mattt
2