SlideShare ist ein Scribd-Unternehmen logo
1 von 119
Downloaden Sie, um offline zu lesen
Paolo Tagliani § Learn to love networking on iOS
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…
Learn to love networking on iOS
What you need to know
HTTP Basics
Learn to love networking on iOS
What you need to know
HTTP Basics
HTTPVerbs	

• 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
What you need to know
Learn to love networking
What you need to know
Objective-C
Learn to love networking
What you need to know
Objective-C
iOS Bootcamp
What you need to know
iOS Bootcamp
What you need to know
iOS Bootcamp
What you need to know
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: CFNetwork
Foundation (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: CFNetwork
Foundation (NSURL* classes)
CFNetwork
BSD Socket
Definition
iOS Bootcamp
Networking in Cocoa: CFNetwork
Foundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
Definition
iOS Bootcamp
Networking in Cocoa: CFNetwork
Foundation (NSURL* classes)
CFNetwork
BSD Socket • Only C code
• Focused on network protocol (HTTP and
FTP)
Definition
iOS Bootcamp
Networking in Cocoa: CFNetwork
Foundation (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: CFNetwork
Foundation (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: CFNetwork
Foundation (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: CFNetwork
Foundation (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: CFNetwork
Foundation (NSURL* classes)
CFNetwork
BSD Socket
Summary
iOS Bootcamp
Networking in Cocoa: CFNetwork
Foundation (NSURL* classes)
CFNetwork
BSD Socket
• CFNetwork is a low-level C API that
provides abstractions over BSD sockets
Summary
iOS Bootcamp
Networking in Cocoa: CFNetwork
Foundation (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: CFNetwork
Foundation (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: CFNetwork
Foundation (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 : Foundation
Foundation (NSURL* classes)
CFNetwork
BSD Socket
URL loading system
Learn to love networking
Networking in cocoa : Foundation
Foundation (NSURL* classes)
CFNetwork
BSD Socket • Set of API written in Objective-C
URL loading system
Learn to love networking
Networking in cocoa : Foundation
Foundation (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 : Foundation
Foundation (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 : Foundation
Foundation (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 : Foundation
Foundation (NSURL* classes)
CFNetwork
BSD Socket
URL loading system
Learn to love networking
Networking in cocoa : Foundation
Foundation (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 : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket • Use 2 different support classes:
NSURLRequest and NSURLResponse.
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Asynchronous connection with delegate: handle request event
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLConnectionFoundation (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 : Foundation
NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLSessionFoundation (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 : Foundation
NSURLSessionFoundation (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 : Foundation
NSURLSessionFoundation (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 : Foundation
NSURLSessionFoundation (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 : Foundation
NSURLSession: types of sessionsFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: 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 : Foundation
NSURLSession: taskFoundation (NSURL* classes)
CFNetwork
BSD Socket
http://www.raywenderlich.com/51127/nsurlsession-tutorial
Learn to love networking on iOS
Networking in cocoa : Foundation
Why use NSURLSessionFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
Why 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 : Foundation
Why 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 : Foundation
Why 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 : Foundation
Why 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 : Foundation
SummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
Learn to love networking on iOS
Networking in cocoa : Foundation
SummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
Learn to love networking on iOS
Networking in cocoa : Foundation
SummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
Learn to love networking on iOS
Networking in cocoa : Foundation
SummaryFoundation (NSURL* classes)
CFNetwork
BSD Socket
You can do anything
BUT
Learn to love networking on iOS
Networking in cocoa : Foundation
SummaryFoundation (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 : Foundation
SummaryFoundation (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 : Foundation
SummaryFoundation (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 : Foundation
SummaryFoundation (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
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
@mattt
(Alamo Fire = AF*)
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
UIActivityIndicatorView
UIProgressView
Learn to love networking on iOS
AFNetworking
UIKit extension
UIRefreshControl
UIWebView
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
Learn to love networking on iOS
AFNetworking
UIKit extension
[imageview setImgeWithURL:HearthImageURL]
Learn to love networking on iOS
AFNetworking
Summary
Learn to love networking on iOS
AFNetworking
Summary
•AFNetworking is powerful
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
Tools
Charles web debugging proxy
Learn to love networking on iOS
Tools
Postman REST client
Learn to love networking on iOS
Tools
JSON Accelerator
Learn to love networking on iOS
Tools
Cocoapods
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)
iOS Bootcamp
@PablosProject http://pragmamark.org/

Weitere ähnliche Inhalte

Was ist angesagt?

Enable oracle database vault
Enable oracle database vaultEnable oracle database vault
Enable oracle database vaultOsama Mustafa
 
VMUGIT UC 2013 - 06 Mike Laverick
VMUGIT UC 2013 - 06 Mike LaverickVMUGIT UC 2013 - 06 Mike Laverick
VMUGIT UC 2013 - 06 Mike LaverickVMUG IT
 
Eouc 12 on 12c osama mustafa
Eouc 12 on 12c osama mustafaEouc 12 on 12c osama mustafa
Eouc 12 on 12c osama mustafaOsama Mustafa
 
Automating the Network
Automating the NetworkAutomating the Network
Automating the NetworkPuppet
 
Enhancing OpenStack FWaaS for real world application
Enhancing OpenStack FWaaS for real world applicationEnhancing OpenStack FWaaS for real world application
Enhancing OpenStack FWaaS for real world applicationopenstackindia
 
What is NetDevOps? How? Leslie Carr PuppetConf 2015
What is NetDevOps? How? Leslie Carr PuppetConf 2015What is NetDevOps? How? Leslie Carr PuppetConf 2015
What is NetDevOps? How? Leslie Carr PuppetConf 2015Leslie Carr
 
Go Faster with Ansible (PHP meetup)
Go Faster with Ansible (PHP meetup)Go Faster with Ansible (PHP meetup)
Go Faster with Ansible (PHP meetup)Richard Donkin
 
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...Cisco DevNet
 
Network Automation with Ansible
Network Automation with AnsibleNetwork Automation with Ansible
Network Automation with AnsibleAnas
 
Network Automation: Ansible 101
Network Automation: Ansible 101Network Automation: Ansible 101
Network Automation: Ansible 101APNIC
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationSuresh Kumar
 
How to run appache spark on windows(in sbt console)
How to run appache spark on windows(in sbt console)How to run appache spark on windows(in sbt console)
How to run appache spark on windows(in sbt console)Ankit Kaneri
 
Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Richard Donkin
 
Installation & configuration of IBM Cloud Orchestrator v2.5
Installation & configuration of IBM Cloud Orchestrator v2.5Installation & configuration of IBM Cloud Orchestrator v2.5
Installation & configuration of IBM Cloud Orchestrator v2.5Paulraj Pappaiah
 
RTP NPUG: Ansible Intro and Integration with ACI
RTP NPUG: Ansible Intro and Integration with ACIRTP NPUG: Ansible Intro and Integration with ACI
RTP NPUG: Ansible Intro and Integration with ACIJoel W. King
 
Inithub.org presentation
Inithub.org presentationInithub.org presentation
Inithub.org presentationAaron Welch
 
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideSquid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideAbhishek Kumar
 
Tips for a Faster Website
Tips for a Faster WebsiteTips for a Faster Website
Tips for a Faster WebsiteRayed Alrashed
 
Writing & Sharing Great Modules - Puppet Camp Boston
Writing & Sharing Great Modules - Puppet Camp BostonWriting & Sharing Great Modules - Puppet Camp Boston
Writing & Sharing Great Modules - Puppet Camp BostonPuppet
 

Was ist angesagt? (20)

Ansible
AnsibleAnsible
Ansible
 
Enable oracle database vault
Enable oracle database vaultEnable oracle database vault
Enable oracle database vault
 
VMUGIT UC 2013 - 06 Mike Laverick
VMUGIT UC 2013 - 06 Mike LaverickVMUGIT UC 2013 - 06 Mike Laverick
VMUGIT UC 2013 - 06 Mike Laverick
 
Eouc 12 on 12c osama mustafa
Eouc 12 on 12c osama mustafaEouc 12 on 12c osama mustafa
Eouc 12 on 12c osama mustafa
 
Automating the Network
Automating the NetworkAutomating the Network
Automating the Network
 
Enhancing OpenStack FWaaS for real world application
Enhancing OpenStack FWaaS for real world applicationEnhancing OpenStack FWaaS for real world application
Enhancing OpenStack FWaaS for real world application
 
What is NetDevOps? How? Leslie Carr PuppetConf 2015
What is NetDevOps? How? Leslie Carr PuppetConf 2015What is NetDevOps? How? Leslie Carr PuppetConf 2015
What is NetDevOps? How? Leslie Carr PuppetConf 2015
 
Go Faster with Ansible (PHP meetup)
Go Faster with Ansible (PHP meetup)Go Faster with Ansible (PHP meetup)
Go Faster with Ansible (PHP meetup)
 
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
 
Network Automation with Ansible
Network Automation with AnsibleNetwork Automation with Ansible
Network Automation with Ansible
 
Network Automation: Ansible 101
Network Automation: Ansible 101Network Automation: Ansible 101
Network Automation: Ansible 101
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
How to run appache spark on windows(in sbt console)
How to run appache spark on windows(in sbt console)How to run appache spark on windows(in sbt console)
How to run appache spark on windows(in sbt console)
 
Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)
 
Installation & configuration of IBM Cloud Orchestrator v2.5
Installation & configuration of IBM Cloud Orchestrator v2.5Installation & configuration of IBM Cloud Orchestrator v2.5
Installation & configuration of IBM Cloud Orchestrator v2.5
 
RTP NPUG: Ansible Intro and Integration with ACI
RTP NPUG: Ansible Intro and Integration with ACIRTP NPUG: Ansible Intro and Integration with ACI
RTP NPUG: Ansible Intro and Integration with ACI
 
Inithub.org presentation
Inithub.org presentationInithub.org presentation
Inithub.org presentation
 
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideSquid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
 
Tips for a Faster Website
Tips for a Faster WebsiteTips for a Faster Website
Tips for a Faster Website
 
Writing & Sharing Great Modules - Puppet Camp Boston
Writing & Sharing Great Modules - Puppet Camp BostonWriting & Sharing Great Modules - Puppet Camp Boston
Writing & Sharing Great Modules - Puppet Camp Boston
 

Ähnlich wie Learn to love networking on iOS

Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaS
Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaSAutoscaling OpenStack Natively with Heat, Ceilometer and LBaaS
Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaSShixiong Shang
 
Binding Objective-C Libraries, Miguel de Icaza
Binding Objective-C Libraries, Miguel de IcazaBinding Objective-C Libraries, Miguel de Icaza
Binding Objective-C Libraries, Miguel de IcazaXamarin
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework CoreAsp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Coremohamed elshafey
 
.NET Core: a new .NET Platform
.NET Core: a new .NET Platform.NET Core: a new .NET Platform
.NET Core: a new .NET PlatformAlex Thissen
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftWan Muzaffar Wan Hashim
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backendDavid Padbury
 
Beefing Up AIR - FITC AMS 2012
Beefing Up AIR - FITC AMS 2012Beefing Up AIR - FITC AMS 2012
Beefing Up AIR - FITC AMS 2012Wouter Verweirder
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101Rami Sayar
 
Networking in Kubernetes
Networking in KubernetesNetworking in Kubernetes
Networking in KubernetesMinhan Xia
 
Coding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCoding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCisco DevNet
 
Containerizing a REST API and Deploying to Kubernetes
Containerizing a REST API and Deploying to KubernetesContainerizing a REST API and Deploying to Kubernetes
Containerizing a REST API and Deploying to KubernetesAshley Roach
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesVagif Abilov
 
Coocoo for Cocoapods
Coocoo for CocoapodsCoocoo for Cocoapods
Coocoo for CocoapodsAllan Davis
 
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...Manuel Garcia
 
JUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinderJUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrindermarekgoldmann
 

Ähnlich wie Learn to love networking on iOS (20)

Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaS
Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaSAutoscaling OpenStack Natively with Heat, Ceilometer and LBaaS
Autoscaling OpenStack Natively with Heat, Ceilometer and LBaaS
 
Binding Objective-C Libraries, Miguel de Icaza
Binding Objective-C Libraries, Miguel de IcazaBinding Objective-C Libraries, Miguel de Icaza
Binding Objective-C Libraries, Miguel de Icaza
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework CoreAsp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Core
 
.NET Core: a new .NET Platform
.NET Core: a new .NET Platform.NET Core: a new .NET Platform
.NET Core: a new .NET Platform
 
Discovering OpenBSD on AWS
Discovering OpenBSD on AWSDiscovering OpenBSD on AWS
Discovering OpenBSD on AWS
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in Swift
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Beefing Up AIR - FITC AMS 2012
Beefing Up AIR - FITC AMS 2012Beefing Up AIR - FITC AMS 2012
Beefing Up AIR - FITC AMS 2012
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Networking in Kubernetes
Networking in KubernetesNetworking in Kubernetes
Networking in Kubernetes
 
Coding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCoding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using Spark
 
Containerizing a REST API and Deploying to Kubernetes
Containerizing a REST API and Deploying to KubernetesContainerizing a REST API and Deploying to Kubernetes
Containerizing a REST API and Deploying to Kubernetes
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class Libraries
 
Coocoo for Cocoapods
Coocoo for CocoapodsCoocoo for Cocoapods
Coocoo for Cocoapods
 
Docker Kubernetes Istio
Docker Kubernetes IstioDocker Kubernetes Istio
Docker Kubernetes Istio
 
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...
Altoros Cloud Foundry Training: hands-on workshop for DevOps, Architects and ...
 
Play framework
Play frameworkPlay framework
Play framework
 
ITB2017 - Keynote
ITB2017 - KeynoteITB2017 - Keynote
ITB2017 - Keynote
 
Rack
RackRack
Rack
 
JUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinderJUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinder
 

Kürzlich hochgeladen

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Learn to love networking on iOS

  • 1. Paolo Tagliani § Learn to love networking on iOS Learn to love networking on iOS
  • 2. 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…
  • 3. Learn to love networking on iOS What you need to know HTTP Basics
  • 4. Learn to love networking on iOS What you need to know HTTP Basics HTTPVerbs • GET • POST • PUT • DELETE • HEAD • … HTTP Response code • 2xx (success) • 4xx (client error) • 5xx (server error) • 1xx (informational) • 3xx (redirection)
  • 5. 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
  • 6. Learn to love networking on iOS What you need to know • Client-server • Cachable • Stateless • Layered
  • 7. Learn to love networking on iOS What you need to know • Client-server • Cachable • Stateless • Layered http://en.wikipedia.org/wiki/Representational_state_transfer
  • 8. Learn to love networking What you need to know
  • 9. Learn to love networking What you need to know Objective-C
  • 10. Learn to love networking What you need to know Objective-C
  • 11. iOS Bootcamp What you need to know
  • 12. iOS Bootcamp What you need to know
  • 13. iOS Bootcamp What you need to know
  • 14. Learn to love networking on iOS Networking in cocoa Foundation (NSURL* classes) CFNetwork BSD Socket
  • 15. Learn to love networking on iOS Networking in cocoa Foundation (NSURL* classes) CFNetwork BSD Socket
  • 16. 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
  • 17. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (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
  • 18. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket Definition
  • 19. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • Only C code Definition
  • 20. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • Only C code • Focused on network protocol (HTTP and FTP) Definition
  • 21. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • Only C code • Focused on network protocol (HTTP and FTP) • Abstractions : streams and socket Definition
  • 22. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • Only C code • Focused on network protocol (HTTP and FTP) • Abstractions : streams and socket Definition
  • 23. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (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
  • 24. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (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;          }   }  
  • 25. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket Summary
  • 26. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • CFNetwork is a low-level C API that provides abstractions over BSD sockets Summary
  • 27. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (NSURL* classes) CFNetwork BSD Socket • CFNetwork is a low-level C API that provides abstractions over BSD sockets • Provide high flexibility Summary
  • 28. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (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
  • 29. iOS Bootcamp Networking in Cocoa: CFNetwork Foundation (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
  • 30. Learn to love networking Networking in cocoa : Foundation Foundation (NSURL* classes) CFNetwork BSD Socket URL loading system
  • 31. Learn to love networking Networking in cocoa : Foundation Foundation (NSURL* classes) CFNetwork BSD Socket • Set of API written in Objective-C URL loading system
  • 32. Learn to love networking Networking in cocoa : Foundation Foundation (NSURL* classes) CFNetwork BSD Socket • Set of API written in Objective-C • High level abstraction for interaction with URL resources URL loading system
  • 33. Learn to love networking Networking in cocoa : Foundation Foundation (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
  • 34. Learn to love networking Networking in cocoa : Foundation Foundation (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
  • 35. Learn to love networking Networking in cocoa : Foundation Foundation (NSURL* classes) CFNetwork BSD Socket URL loading system
  • 36. Learn to love networking Networking in cocoa : Foundation Foundation (NSURL* classes) CFNetwork BSD Socket URL loading system
  • 37. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnection or NSURLSession? Foundation (NSURL* classes) CFNetwork BSD Socket
  • 38. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnection or NSURLSession? Foundation (NSURL* classes) CFNetwork BSD Socket
  • 39. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnection or NSURLSession? Foundation (NSURL* classes) CFNetwork BSD Socket
  • 40. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket
  • 41. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse.
  • 42. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket • Use 2 different support classes: NSURLRequest and NSURLResponse. • Most of the setup is made on NSURLRequest. It manages:
  • 43. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 44. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 45. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 46. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 47. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 48. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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)
  • 49. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket
  • 50. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket • Most flexible method for retrieving content of URL.
  • 51. 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:
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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];  
  • 56. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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];  
  • 57. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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];  
  • 58. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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];
  • 59. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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]); }
  • 60. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (NSURL* classes) CFNetwork BSD Socket Asynchronous connection with delegate: handle request event
  • 61. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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)
  • 62. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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 }]
  • 63. Learn to love networking on iOS Networking in cocoa : Foundation NSURLConnectionFoundation (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
  • 64. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSessionFoundation (NSURL* classes) CFNetwork BSD Socket
  • 65. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSessionFoundation (NSURL* classes) CFNetwork BSD Socket • The NSURLSession class and related classes provide an API for downloading content via HTTP.
  • 66. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSessionFoundation (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).
  • 67. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSessionFoundation (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.
  • 68. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSessionFoundation (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.
  • 69. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: types of sessionsFoundation (NSURL* classes) CFNetwork BSD Socket
  • 70. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: types of sessionsFoundation (NSURL* classes) CFNetwork BSD Socket • Default sessions use a persistent disk-based cache and store credentials in the user’s keychain.
  • 71. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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.
  • 72. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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.
  • 73. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: taskFoundation (NSURL* classes) CFNetwork BSD Socket
  • 74. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: taskFoundation (NSURL* classes) CFNetwork BSD Socket • Each session is composed by a number of task. A task is a simple HTTP network operation.
  • 75. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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:
  • 76. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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.
  • 77. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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.
  • 78. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: 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.
  • 79. Learn to love networking on iOS Networking in cocoa : Foundation NSURLSession: taskFoundation (NSURL* classes) CFNetwork BSD Socket http://www.raywenderlich.com/51127/nsurlsession-tutorial
  • 80. Learn to love networking on iOS Networking in cocoa : Foundation Why use NSURLSessionFoundation (NSURL* classes) CFNetwork BSD Socket
  • 81. Learn to love networking on iOS Networking in cocoa : Foundation Why 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.
  • 82. Learn to love networking on iOS Networking in cocoa : Foundation Why 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.
  • 83. Learn to love networking on iOS Networking in cocoa : Foundation Why 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.
  • 84. Learn to love networking on iOS Networking in cocoa : Foundation Why 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).
  • 85. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket
  • 86. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket You can do anything
  • 87. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket You can do anything BUT
  • 88. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket You can do anything BUT
  • 89. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket You can do anything BUT •Build a network stack for your application can be difficult.
  • 90. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (NSURL* classes) CFNetwork BSD Socket You can do anything BUT •Build a network stack for your application can be difficult.
  • 91. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (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)
  • 92. Learn to love networking on iOS Networking in cocoa : Foundation SummaryFoundation (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)
  • 93. Learn to love networking on iOS AFNetworking
  • 94. Learn to love networking on iOS AFNetworking •11.00+ stars •3.000+ forks •1.500+ commits •1300+ closed issues •130 contributors
  • 95. Learn to love networking on iOS AFNetworking @mattt (Alamo Fire = AF*)
  • 96. 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
  • 97. Learn to love networking on iOS AFNetworking Structure AFURLSessionTask Download Upload Data AFURLSessionManager AFHTTPSessionManager NSURLConnection AFURLConnectionOperation AFHTTPRequestOperation AFHTTPRequestOperationManager
  • 98. 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;
  • 99. 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
  • 100. 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
  • 101. Learn to love networking on iOS AFNetworking Serializer extension •MsgPack •CSV / TSV •vCard •vCal •WebP
  • 102. Learn to love networking on iOS AFNetworking Reachability •Monitor reachability on: •IP addresses •URL •Domain •Support different type of connection •3G •Wifi
  • 103. Learn to love networking on iOS AFNetworking UIKit extension UIActivityIndicatorView UIProgressView
  • 104. Learn to love networking on iOS AFNetworking UIKit extension UIRefreshControl UIWebView
  • 105. Learn to love networking on iOS AFNetworking UIKit extension UIButton UIImageView Auto download Caching Operation management
  • 106. Learn to love networking on iOS AFNetworking UIKit extension
  • 107. Learn to love networking on iOS AFNetworking UIKit extension [imageview setImgeWithURL:HearthImageURL]
  • 108. Learn to love networking on iOS AFNetworking Summary
  • 109. Learn to love networking on iOS AFNetworking Summary •AFNetworking is powerful
  • 110. Learn to love networking on iOS AFNetworking Summary •AFNetworking is powerful •Lots of the common task already covered
  • 111. Learn to love networking on iOS AFNetworking Summary •AFNetworking is powerful •Lots of the common task already covered •Drag’n drop solution
  • 112. Learn to love networking on iOS AFNetworking Summary •AFNetworking is powerful •Lots of the common task already covered •Drag’n drop solution DEMO TIME
  • 113. Learn to love networking on iOS Tools Charles web debugging proxy
  • 114. Learn to love networking on iOS Tools Postman REST client
  • 115. Learn to love networking on iOS Tools JSON Accelerator
  • 116. Learn to love networking on iOS Tools Cocoapods
  • 117. 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
  • 118. 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)