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 AFNetworking 2.0
Search
Kod.io
October 26, 2013
Programming
2
210
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
61
You should learn Go!
kodio
1
120
Daha iyi Geliştirme Ortamı için: Vagrant
kodio
0
63
Reactive Principles
kodio
1
130
AngularJS vs. Ember.js vs. Backbone.js
kodio
1
500
iBeacons: Interacting with the physical world using Bluetooth LE
kodio
1
84
Go: a simple programming environment
kodio
2
150
Why I had to write a client side framework for Koding.
kodio
1
88
Sure, I could learn Objective-C but...
kodio
1
80
Other Decks in Programming
See All in Programming
Goのmultiple errorsについて (2024年4月版)
syumai
1
320
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
620
Git Lint
bkuhlmann
4
750
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
920
PHPはいつから死んでいるかの調査
chiroruxx
1
360
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
220
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
180
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
6
880
雑に思考を整理する技術と効能
konifar
58
28k
SwiftUI Performance 不要なViewの再描画と更新を抑える
bigamitiongit
1
160
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
470
Site Reliability Engineering for GMO
pyama86
7
1k
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
331
56k
Git: the NoSQL Database
bkeepers
PRO
422
63k
Debugging Ruby Performance
tmm1
70
11k
Building an army of robots
kneath
300
41k
Statistics for Hackers
jakevdp
789
220k
It's Worth the Effort
3n
180
27k
Designing Experiences People Love
moore
136
23k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
273
13k
Imperfection Machines: The Place of Print at Facebook
scottboms
259
12k
Clear Off the Table
cherdarchuk
83
310k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
24
2.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
220
21k
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