Learn to love networking on iOS
#pragma me
• Paolo Tagliani (@PablosProject)
• iOS Developer @Superpartes Innovation Campus
• Founder of #pragma mark
• various stuff…
!
• @PablosProject
• http://www.pablosproject.com
• https://www.facebook.com/paolo.tagliani
• https://github.com/pablosproject
• More…
Learn to love networking on iOS
What you need to know
HTTP Basics
HTTP Verbs • GET • POST • PUT • DELETE • HEAD • …
HTTP Response code • 2xx (success) • 4xx (client error) • 5xx (server error) • 1xx (informational) • 3xx (redirection)
Learn to love networking on iOS
What you need to know
HTTP Basics
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
Learn to love networking on iOS
What you need to know
• Client-server
• Cachable
• Stateless
• Layered
Learn to love networking on iOS
What you need to know
• Client-server
• Cachable
• Stateless
• Layered
http://en.wikipedia.org/wiki/Representational_state_transfer
Learn to love networking on iOS
Networking in cocoa
Foundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa
Foundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa
Foundation (NSURL* classes)
CFNetwork
BSD Socket
https://www.freebsd.org/doc/en/books/developers-handbook/sockets.html
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
CFNetwork is a low-level, high-performance framework that gives you the ability to have detailed control over the protocol stack. It is an extension to BSD sockets, the standard socket abstraction API that provides objects to simplify tasks such as communicating with FTP and HTTP servers or resolving DNS hosts. CFNetwork is based, both physically and theoretically, on BSD sockets. (https://developer.apple.com/library/ios/
documentation/Networking/Conceptual/CFNetwork/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001132)
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
• Focused on network protocol (HTTP and FTP)
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
• Focused on network protocol (HTTP and FTP)
• Abstractions : streams and socket
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
• Focused on network protocol (HTTP and FTP)
• Abstractions : streams and socket
Definition
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
CFStringRef url = CFSTR("http://www.apple.com"); !CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL); !CFStringRef requestMethod = CFSTR("GET"); ! !CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, ! requestMethod, myUrl, kCFHTTPVersion1_1); !CFHTTPMessageSetBody(myRequest, bodyData); !CFHTTPMessageSetHeaderFieldValue(myRequest, headerField, value); ! !CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest); ! !CFReadStreamOpen(myReadStream);
Example: communicate with HTTP server
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
Example: communicate with HTTP server
//Setting the client for the stream Boolean CFReadStreamSetClient ( CFReadStreamRef stream, CFOptionFlags streamEvents, CFReadStreamClientCallBack clientCB, CFStreamClientContext *clientContext ); !//Callback to handle stream Events !void myCallBack (CFReadStreamRef stream, CFStreamEventType event, void *myPtr) { switch(event) { case kCFStreamEventHasBytesAvailable: {…} break; case kCFStreamEventErrorOccurred: {…} break; case kCFStreamEventEndEncountered: {…} break; } }
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
• CFNetwork is a low-level C API that provides abstractions over BSD sockets
Summary
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
• CFNetwork is a low-level C API that provides abstractions over BSD sockets
• Provide high flexibility
Summary
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
• CFNetwork is a low-level C API that provides abstractions over BSD sockets
• Provide high flexibility
• As you write your code, it is recommended that you prefer the use of higher-level frameworks over lower-level frameworks whenever possible.
Summary
iOS Bootcamp
Networking in Cocoa: CFNetworkFoundation (NSURL* classes)
CFNetwork
BSD Socket
• CFNetwork is a low-level C API that provides abstractions over BSD sockets
• Provide high flexibility
• As you write your code, it is recommended that you prefer the use of higher-level frameworks over lower-level frameworks whenever possible.
Summary
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket • Set of API written in Objective-C
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket • Set of API written in Objective-C
• High level abstraction for interaction with URL resources
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket • Set of API written in Objective-C
• High level abstraction for interaction with URL resources
• At the heart of this technology is the NSURL class, which lets your app manipulate URLs and the resources they refer to.
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket • Set of API written in Objective-C
• High level abstraction for interaction with URL resources
• At the heart of this technology is the NSURL class, which lets your app manipulate URLs and the resources they refer to.
• Together these classes (NSURL*) are referred to as the URL loading system.
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket
URL loading system
Learn to love networking
Networking in cocoa : FoundationFoundation (NSURL* classes)
CFNetwork
BSD Socket
URL loading system
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnection or NSURLSession?Foundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnection or NSURLSession?Foundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnection or NSURLSession?Foundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
• Cache policy
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
• Cache policy
• HTTP Parameters and header fiels
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
• Cache policy
• HTTP Parameters and header fiels
• Timeout
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
• Cache policy
• HTTP Parameters and header fiels
• Timeout
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
• Most of the setup is made on NSURLRequest. It manages:
• The request URL
• Cache policy
• HTTP Parameters and header fiels
• Timeout
• NSURLResponse manage the response information (ex. HTTP status code)
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Most flexible method for retrieving content of URL.
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Most flexible method for retrieving content of URL.
• Use three different ways for retrieving the content:
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Most flexible method for retrieving content of URL.
• Use three different ways for retrieving the content:
• Synchronous call
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Most flexible method for retrieving content of URL.
• Use three different ways for retrieving the content:
• Synchronous call
• Asynchronous with delegate
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Most flexible method for retrieving content of URL.
• Use three different ways for retrieving the content:
• Synchronous call
• Asynchronous with delegate
• Asynchronous with block
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Synchronous connection
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; !//Create a response for the request NSURLResponse *response; !NSError *error; !//Send the request [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Synchronous connection
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; !//Create a response for the request NSURLResponse *response; !NSError *error; !//Send the request [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Synchronous connection
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; !//Create a response for the request NSURLResponse *response; !NSError *error; !//Send the request [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with delegate: create the request
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // Create the NSMutableData to hold the received data. // receivedData is an instance variable declared elsewhere. receivedData = [NSMutableData dataWithCapacity: 0]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with delegate: handle request event! - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // This method is called when the server has determined that it // has enough information to create the NSURLResponse object. // receivedData is an instance variable declared elsewhere. [receivedData setLength:0]; } ... - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the new data to receivedData. // receivedData is an instance variable declared elsewhere. [receivedData appendData:data]; } ... - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { //Clean all variables theConnection = nil; receivedData = nil; // inform the user NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); }
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with delegate: handle request event
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with delegate: handle request event
NSURLConnectionDelegate (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/
Reference.html) !!
NSURLConnectionDataDelegate (https://developer.apple.com/library/ios/DOCUMENTATION/Foundation/Reference/NSURLConnectionDataDelegate_protocol/
Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDataDelegate)
!
NSURLConnectionDownloadDelegate (https://developer.apple.com/library/ios/DOCUMENTATION/Foundation/Reference/NSURLConnectionDownloadDelegate_Protocol/
NSURLConnectionDownloadDelegate/NSURLConnectionDownloadDelegate.html#//apple_ref/occ/intf/NSURLConnectionDownloadDelegate)
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with completion block
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; //Init an operation queue on which run the completion handler NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //Handle error or return data }]
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with completion block
// Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; //Init an operation queue on which run the completion handler NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //Handle error or return data }]
Only for iOS 5.0+ Difficult to handle authentication Less flexibility
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
• The NSURLSession class and related classes provide an API for downloading content via HTTP.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
• The NSURLSession class and related classes provide an API for downloading content via HTTP.
• Work transparently with delegate and with completion callbacks (blocks).
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
• The NSURLSession class and related classes provide an API for downloading content via HTTP.
• Work transparently with delegate and with completion callbacks (blocks).
• The NSURLSession API provides status and progress properties.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
• The NSURLSession class and related classes provide an API for downloading content via HTTP.
• Work transparently with delegate and with completion callbacks (blocks).
• The NSURLSession API provides status and progress properties.
• It supports canceling, restarting (resuming), and suspending tasks, and it provides the ability to resume suspended, canceled, or failed downloads.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: types of sessionsFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: types of sessionsFoundation (NSURL* classes)
CFNetwork
BSD Socket
• Default sessions use a persistent disk-based cache and store credentials in the user’s keychain.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: types of sessionsFoundation (NSURL* classes)
CFNetwork
BSD Socket
• Default sessions use a persistent disk-based cache and store credentials in the user’s keychain.
• Ephemeral sessions do not store any data to disk; all caches, credential stores, and so on are kept in RAM and tied to the session.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: types of sessionsFoundation (NSURL* classes)
CFNetwork
BSD Socket
• Default sessions use a persistent disk-based cache and store credentials in the user’s keychain.
• Ephemeral sessions do not store any data to disk; all caches, credential stores, and so on are kept in RAM and tied to the session.
• Background sessions are similar to default sessions, except that a separate process handles all data transfers.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
• Different types of task:
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
• Different types of task:
• Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
• Different types of task:
• Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server.
• Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
• Different types of task:
• Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server.
• Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.
• Upload tasks send data (usually in the form of a file), and support background uploads while the app is not running.
Learn to love networking on iOS
Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket
http://www.raywenderlich.com/51127/nsurlsession-tutorial
Learn to love networking on iOS
Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Background task: using this API the app creates automatically for you a daemon (on OSX) and wake up your app several time (on iOS) to complete background transfers.
Learn to love networking on iOS
Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Background task: using this API the app creates automatically for you a daemon (on OSX) and wake up your app several time (on iOS) to complete background transfers.
• Encapsulate network logic: each session manage its task. You can suspend, resume, and control progress of every task of the session.
Learn to love networking on iOS
Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Background task: using this API the app creates automatically for you a daemon (on OSX) and wake up your app several time (on iOS) to complete background transfers.
• Encapsulate network logic: each session manage its task. You can suspend, resume, and control progress of every task of the session.
• Easy configuration: with NSURLSessionConfiguration: configure once and share configuration for all task.
Learn to love networking on iOS
Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Background task: using this API the app creates automatically for you a daemon (on OSX) and wake up your app several time (on iOS) to complete background transfers.
• Encapsulate network logic: each session manage its task. You can suspend, resume, and control progress of every task of the session.
• Easy configuration: with NSURLSessionConfiguration: configure once and share configuration for all task.
• Uploads and downloads through the file system: This encourages the separation of the data (file contents) from the metadata (the URL and settings).
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
•Build a network stack for your application can be difficult.
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
•Build a network stack for your application can be difficult.
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
•Build a network stack for your application can be difficult.
•There’s no a drop-in solution (i.e. reinvent the wheel every time)
Learn to love networking on iOS
Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
•Build a network stack for your application can be difficult.
•There’s no a drop-in solution (i.e. reinvent the wheel every time)
Learn to love networking on iOS
AFNetworking
•11.00+ stars •3.000+ forks •1.500+ commits •1300+ closed issues •130 contributors
Learn to love networking on iOS
AFNetworking
AFNetworking 2.0• iOS 6+ & Mac OS X 10.8+
• Xcode 5
• NSURLSession & NSURLConnection
• Serialization Modules
• UIKit Extensions
• Real-time
Learn to love networking on iOS
AFNetworking
Structure
AFURLSessionTask
Download Upload Data
AFURLSessionManager
AFHTTPSessionManager
NSURLConnection
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
Learn to love networking on iOS
AFNetworking GET a resource
AFHTTPRequestOperationManager
- (AFHTTPRequestOperation *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure;
Learn to love networking on iOS
AFNetworking GET a resource
- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
AFHTTPSessionManager
Learn to love networking on iOS
AFNetworking Serialization
Request serializer Response serializer• HTTP • JSON • Property List
• HTTP • JSON • XML parser • XML document (OSX) • Property List • Image
Learn to love networking on iOS
AFNetworking Serializer extension
• MsgPack
• CSV / TSV
• vCard
• vCal
• WebP
Learn to love networking on iOS
AFNetworking Reachability
• Monitor reachability on:
• IP addresses
• URL
• Domain
• Support different type of connection
• 3G
• Wifi
Learn to love networking on iOS
AFNetworking UIKit extension
UIButton
UIImageView
Auto download
Caching
Operation management
Learn to love networking on iOS
AFNetworking UIKit extension
[imageview setImgeWithURL:HearthImageURL]
Learn to love networking on iOS
AFNetworking Summary
• AFNetworking is powerful
• Lots of the common task already covered
Learn to love networking on iOS
AFNetworking Summary
• AFNetworking is powerful
• Lots of the common task already covered
• Drag’n drop solution
Learn to love networking on iOS
AFNetworking Summary
• AFNetworking is powerful
• Lots of the common task already covered
• Drag’n drop solution
DEMO TIME
Learn to love networking on iOS
References
• Apple documentation
• CFNetwork Programming guide
• URL Loading System Programming guide
!
• Ray Wanderlich
• AFNetworking 2.0 Tutorial
• NSURLSession Tutorial
Learn to love networking on iOS
References
• WWDC Video
• WWDC 2013 Session 705 “What’s New in Foundation Networking”
!
• NSScreencast
• Episode #91: AFNetworking2.0
• Episode #81: Networking in iOS 7
!
• NSHipster (@mattt)
• AFNetworking 2.0
• AFNetworking: the Definitive Guide (TBA)
Top Related