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
140
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
git worktree × Claude Code × MCP ~生成AI時代の並列開発フロー~
hisuzuya
0
170
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
560
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
320
ASP.NETアプリケーションのモダナイズ インフラ編
tomokusaba
1
390
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
610
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
2
630
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
210
コード書くの好きな人向けAIコーディング活用tips #orestudy
77web
3
330
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
240
XSLTで作るBrainfuck処理系
makki_d
0
210
Go Modules: From Basics to Beyond / Go Modulesの基本とその先へ
kuro_kurorrr
0
120
XP, Testing and ninja testing
m_seki
2
130
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
14k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Practical Orchestrator
shlominoach
188
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
330
24k
Facilitating Awesome Meetings
lara
54
6.4k
Designing Experiences People Love
moore
142
24k
Become a Pro
speakerdeck
PRO
28
5.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
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