Top Banner
Paolo Tagliani § Learn to love networking on iOS Learn to love networking on iOS
119
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Learn to love networking on iOS

Paolo Tagliani § Learn to love networking on iOS

Learn to love networking on iOS

Page 2: Learn to love networking on iOS

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…

Page 3: Learn to love networking on iOS

Learn to love networking on iOS

What you need to know

HTTP Basics

Page 4: Learn to love networking on iOS

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)

Page 5: Learn to love networking on iOS

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

Page 6: Learn to love networking on iOS

Learn to love networking on iOS

What you need to know

• Client-server

• Cachable

• Stateless

• Layered

Page 7: Learn to love networking on iOS

Learn to love networking on iOS

What you need to know

• Client-server

• Cachable

• Stateless

• Layered

http://en.wikipedia.org/wiki/Representational_state_transfer

Page 8: Learn to love networking on iOS

Learn to love networking

What you need to know

Page 9: Learn to love networking on iOS

Learn to love networking

What you need to know

Objective-C

Page 10: Learn to love networking on iOS

Learn to love networking

What you need to know

Objective-C

Page 11: Learn to love networking on iOS

iOS Bootcamp

What you need to know

Page 12: Learn to love networking on iOS

iOS Bootcamp

What you need to know

Page 13: Learn to love networking on iOS

iOS Bootcamp

What you need to know

Page 14: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa

Foundation (NSURL* classes)

CFNetwork

BSD Socket

Page 15: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa

Foundation (NSURL* classes)

CFNetwork

BSD Socket

Page 16: Learn to love networking on iOS

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

Page 17: Learn to love networking on iOS

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

Page 18: Learn to love networking on iOS

iOS Bootcamp

Networking in Cocoa: CFNetworkFoundation (NSURL* classes)

CFNetwork

BSD Socket

Definition

Page 19: Learn to love networking on iOS

iOS Bootcamp

Networking in Cocoa: CFNetworkFoundation (NSURL* classes)

CFNetwork

BSD Socket • Only C code

Definition

Page 20: Learn to love networking on iOS

iOS Bootcamp

Networking in Cocoa: CFNetworkFoundation (NSURL* classes)

CFNetwork

BSD Socket • Only C code

• Focused on network protocol (HTTP and FTP)

Definition

Page 21: Learn to love networking on iOS

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

Page 22: Learn to love networking on iOS

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

Page 23: Learn to love networking on iOS

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

Page 24: Learn to love networking on iOS

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;          }  }  

Page 25: Learn to love networking on iOS

iOS Bootcamp

Networking in Cocoa: CFNetworkFoundation (NSURL* classes)

CFNetwork

BSD Socket

Summary

Page 26: Learn to love networking on iOS

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

Page 27: Learn to love networking on iOS

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

Page 28: Learn to love networking on iOS

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

Page 29: Learn to love networking on iOS

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

Page 30: Learn to love networking on iOS

Learn to love networking

Networking in cocoa : FoundationFoundation (NSURL* classes)

CFNetwork

BSD Socket

URL loading system

Page 31: Learn to love networking on iOS

Learn to love networking

Networking in cocoa : FoundationFoundation (NSURL* classes)

CFNetwork

BSD Socket • Set of API written in Objective-C

URL loading system

Page 32: Learn to love networking on iOS

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

Page 33: Learn to love networking on iOS

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

Page 34: Learn to love networking on iOS

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

Page 35: Learn to love networking on iOS

Learn to love networking

Networking in cocoa : FoundationFoundation (NSURL* classes)

CFNetwork

BSD Socket

URL loading system

Page 36: Learn to love networking on iOS

Learn to love networking

Networking in cocoa : FoundationFoundation (NSURL* classes)

CFNetwork

BSD Socket

URL loading system

Page 37: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : Foundation

NSURLConnection or NSURLSession?Foundation (NSURL* classes)

CFNetwork

BSD Socket

Page 38: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : Foundation

NSURLConnection or NSURLSession?Foundation (NSURL* classes)

CFNetwork

BSD Socket

Page 39: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : Foundation

NSURLConnection or NSURLSession?Foundation (NSURL* classes)

CFNetwork

BSD Socket

Page 40: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 41: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)

CFNetwork

BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.

Page 42: Learn to love networking on iOS

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:

Page 43: Learn to love networking on iOS

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

Page 44: Learn to love networking on iOS

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

Page 45: Learn to love networking on iOS

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

Page 46: Learn to love networking on iOS

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

Page 47: Learn to love networking on iOS

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

Page 48: Learn to love networking on iOS

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)

Page 49: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : Foundation

NSURLConnectionFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 50: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : Foundation

NSURLConnectionFoundation (NSURL* classes)

CFNetwork

BSD Socket • Most flexible method for retrieving content of URL.

Page 51: Learn to love networking on iOS

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:

Page 52: Learn to love networking on iOS

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

Page 53: Learn to love networking on iOS

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

Page 54: Learn to love networking on iOS

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

Page 55: Learn to love networking on iOS

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];  

Page 56: Learn to love networking on iOS

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];  

Page 57: Learn to love networking on iOS

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];  

Page 58: Learn to love networking on iOS

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];

Page 59: Learn to love networking on iOS

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]); }

Page 60: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLConnectionFoundation (NSURL* classes)

CFNetwork

BSD Socket

Asynchronous connection with delegate: handle request event

Page 61: Learn to love networking on iOS

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)

Page 62: Learn to love networking on iOS

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 }]

Page 63: Learn to love networking on iOS

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

Page 64: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLSessionFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 65: Learn to love networking on iOS

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.

Page 66: Learn to love networking on iOS

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).

Page 67: Learn to love networking on iOS

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.

Page 68: Learn to love networking on iOS

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.

Page 69: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLSession: types of sessionsFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 70: Learn to love networking on iOS

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.

Page 71: Learn to love networking on iOS

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.

Page 72: Learn to love networking on iOS

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.

Page 73: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 74: Learn to love networking on iOS

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.

Page 75: Learn to love networking on iOS

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:

Page 76: Learn to love networking on iOS

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.

Page 77: Learn to love networking on iOS

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.

Page 78: Learn to love networking on iOS

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.

Page 79: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationNSURLSession: taskFoundation (NSURL* classes)

CFNetwork

BSD Socket

http://www.raywenderlich.com/51127/nsurlsession-tutorial

Page 80: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationWhy use NSURLSessionFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 81: Learn to love networking on iOS

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.

Page 82: Learn to love networking on iOS

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.

Page 83: Learn to love networking on iOS

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.

Page 84: Learn to love networking on iOS

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).

Page 85: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)

CFNetwork

BSD Socket

Page 86: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)

CFNetwork

BSD Socket

You can do anything

Page 87: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)

CFNetwork

BSD Socket

You can do anything

BUT

Page 88: Learn to love networking on iOS

Learn to love networking on iOS

Networking in cocoa : FoundationSummaryFoundation (NSURL* classes)

CFNetwork

BSD Socket

You can do anything

BUT

Page 89: Learn to love networking on iOS

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.

Page 90: Learn to love networking on iOS

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.

Page 91: Learn to love networking on iOS

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)

Page 92: Learn to love networking on iOS

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)

Page 93: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking

Page 94: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking

•11.00+ stars •3.000+ forks •1.500+ commits •1300+ closed issues •130 contributors

Page 95: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking

@mattt

(Alamo Fire = AF*)

Page 96: Learn to love networking on iOS

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

Page 97: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking

Structure

AFURLSessionTask

Download Upload Data

AFURLSessionManager

AFHTTPSessionManager

NSURLConnection

AFURLConnectionOperation

AFHTTPRequestOperation

AFHTTPRequestOperationManager

Page 98: Learn to love networking on iOS

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;

Page 99: Learn to love networking on iOS

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

Page 100: Learn to love networking on iOS

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

Page 101: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Serializer extension

• MsgPack

• CSV / TSV

• vCard

• vCal

• WebP

Page 102: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Reachability

• Monitor reachability on:

• IP addresses

• URL

• Domain

• Support different type of connection

• 3G

• Wifi

Page 103: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking UIKit extension

UIActivityIndicatorView

UIProgressView

Page 104: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking UIKit extension

UIRefreshControl

UIWebView

Page 105: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking UIKit extension

UIButton

UIImageView

Auto download

Caching

Operation management

Page 106: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking UIKit extension

Page 107: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking UIKit extension

[imageview setImgeWithURL:HearthImageURL]

Page 108: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Summary

Page 109: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Summary

• AFNetworking is powerful

Page 110: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Summary

• AFNetworking is powerful

• Lots of the common task already covered

Page 111: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Summary

• AFNetworking is powerful

• Lots of the common task already covered

• Drag’n drop solution

Page 112: Learn to love networking on iOS

Learn to love networking on iOS

AFNetworking Summary

• AFNetworking is powerful

• Lots of the common task already covered

• Drag’n drop solution

DEMO TIME

Page 113: Learn to love networking on iOS

Learn to love networking on iOS

Tools Charles web debugging proxy

Page 114: Learn to love networking on iOS

Learn to love networking on iOS

Tools Postman REST client

Page 115: Learn to love networking on iOS

Learn to love networking on iOS

Tools JSON Accelerator

Page 116: Learn to love networking on iOS

Learn to love networking on iOS

Tools Cocoapods

Page 117: Learn to love networking on iOS

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

Page 118: Learn to love networking on iOS

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)

Page 119: Learn to love networking on iOS

iOS Bootcamp

@PablosProject http://pragmamark.org/