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
310
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
64
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
93
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
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
330
Spring gRPC について / About Spring gRPC
mackey0225
0
110
定理証明プラットフォーム lapisla.net
abap34
1
610
SpringBoot3.4の構造化ログ #kanjava
irof
1
460
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
3.6k
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
380
Package Traits
ikesyo
2
220
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
28
6.1k
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
140
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
620
ESLintプラグインを使用してCDKのセオリーを適用する
yamanashi_ren01
2
300
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.6k
Featured
See All Featured
Become a Pro
speakerdeck
PRO
26
5.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
6
210
Navigating Team Friction
lara
183
15k
Thoughts on Productivity
jonyablonski
68
4.4k
Scaling GitHub
holman
459
140k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
Being A Developer After 40
akosma
89
590k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
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