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
280
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
63
You should learn Go!
kodio
1
130
Daha iyi Geliştirme Ortamı için: Vagrant
kodio
0
64
Reactive Principles
kodio
1
140
AngularJS vs. Ember.js vs. Backbone.js
kodio
1
500
iBeacons: Interacting with the physical world using Bluetooth LE
kodio
1
90
Go: a simple programming environment
kodio
2
160
Why I had to write a client side framework for Koding.
kodio
1
91
Sure, I could learn Objective-C but...
kodio
1
84
Other Decks in Programming
See All in Programming
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.4k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
1.1k
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Quine, Polyglot, 良いコード
qnighy
4
650
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
720
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Better Code Design in PHP
afilina
PRO
0
130
flutterkaigi_2024.pdf
kyoheig3
0
170
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
6
1.6k
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Teambox: Starting and Learning
jrom
133
8.8k
Docker and Python
trallard
40
3.1k
Fireside Chat
paigeccino
34
3k
Rails Girls Zürich Keynote
gr2m
94
13k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Statistics for Hackers
jakevdp
796
220k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
How STYLIGHT went responsive
nonsquared
95
5.2k
GraphQLとの向き合い方2022年版
quramy
43
13k
It's Worth the Effort
3n
183
27k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
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