Inherits from NSObject
Conforms to NSCopying
NSSecureCoding
Declared in AFHTTPRequestOperationManager.h

Overview

AFHTTPRequestOperationManager encapsulates the common patterns of communicating with a web application over HTTP, including request creation, response serialization, network reachability monitoring, and security, as well as request operation management.

Subclassing Notes

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass AFHTTPSessionManager, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

For developers targeting iOS 6 or Mac OS X 10.8 or earlier, AFHTTPRequestOperationManager may be used to similar effect.

Methods to Override

To change the behavior of all request operation construction for an AFHTTPRequestOperationManager subclass, override HTTPRequestOperationWithRequest:success:failure.

Serialization

Requests created by an HTTP client will contain default headers and encode parameters according to the requestSerializer property, which is an object conforming to AFURLRequestSerialization.

Responses received from the server are automatically validated and serialized by the responseSerializers property, which is an object conforming to AFURLResponseSerialization

URL Construction Using Relative Paths

For HTTP convenience methods, the request serializer constructs URLs from the path relative to the baseURL, using NSURL +URLWithString:relativeToURL:, when provided. If baseURL is nil, path needs to resolve to a valid NSURL object using NSURL +URLWithString:.

Below are a few examples of how baseURL and relative paths interact:

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  // http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

Also important to note is that a trailing slash will be added to any baseURL without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash.

Network Reachability Monitoring

Network reachability status and change monitoring is available through the reachabilityManager property. Applications may choose to monitor network reachability conditions in order to prevent or suspend any outbound requests. See AFNetworkReachabilityManager for more details.

NSSecureCoding & NSCopying Caveats

AFHTTPRequestOperationManager conforms to the NSSecureCoding and NSCopying protocols, allowing operations to be archived to disk, and copied in memory, respectively. There are a few minor caveats to keep in mind, however:

  • Archives and copies of HTTP clients will be initialized with an empty operation queue.
  • NSSecureCoding cannot serialize / deserialize block properties, so an archive of an HTTP client will not include any reachability callback block that may be set.

Tasks

Other Methods

  •   baseURL

    The URL used to monitor reachability, and construct requests from relative paths in methods like requestWithMethod:URLString:parameters:, and the GET / POST / et al. convenience methods.

    property
  •   requestSerializer

    Requests created with requestWithMethod:URLString:parameters: & multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock: are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of AFHTTPRequestSerializer, which serializes query string parameters for GET, HEAD, and DELETE requests, or otherwise URL-form-encodes HTTP message bodies.

    property
  •   responseSerializer

    Responses sent from the server in data tasks created with dataTaskWithRequest:success:failure: and run using the GET / POST / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to a JSON serializer, which serializes data from responses with a application/json MIME type, and falls back to the raw data object. The serializer validates the status code to be in the 2XX range, denoting success. If the response serializer generates an error in -responseObjectForResponse:data:error:, the failure callback of the session task or request operation will be executed; otherwise, the success callback will be executed.

    property
  •   operationQueue

    The operation queue on which request operations are scheduled and run.

    property

Managing URL Credentials

Managing Security Policy

  •   securityPolicy

    The security policy used by created request operations to evaluate server trust for secure connections. AFHTTPRequestOperationManager uses the defaultPolicy unless otherwise specified.

    property

Managing Network Reachability

Managing Callback Queues

  •   completionQueue

    The dispatch queue for the completionBlock of request operations. If NULL (default), the main queue is used.

    property
  •   completionGroup

    The dispatch group for the completionBlock of request operations. If NULL (default), a private dispatch group is used.

    property

Creating and Initializing HTTP Clients

  • + manager

    Creates and returns an AFHTTPRequestOperationManager object.

  • – initWithBaseURL:

    Initializes an AFHTTPRequestOperationManager object with the specified base URL.

Managing HTTP Request Operations

Making HTTP Requests

Properties

baseURL

@property (readonly, nonatomic, strong) NSURL *baseURL
Discussion

The URL used to monitor reachability, and construct requests from relative paths in methods like requestWithMethod:URLString:parameters:, and the GET / POST / et al. convenience methods.

Declared In

AFHTTPRequestOperationManager.h

completionGroup

@property (nonatomic, strong) dispatch_group_t completionGroup
Discussion

The dispatch group for the completionBlock of request operations. If NULL (default), a private dispatch group is used.

Declared In

AFHTTPRequestOperationManager.h

completionQueue

@property (nonatomic, strong) dispatch_queue_t completionQueue
Discussion

The dispatch queue for the completionBlock of request operations. If NULL (default), the main queue is used.

Declared In

AFHTTPRequestOperationManager.h

credential

@property (nonatomic, strong) NSURLCredential *credential
Discussion

The credential used by request operations for authentication challenges.

Declared In

AFHTTPRequestOperationManager.h

operationQueue

@property (nonatomic, strong) NSOperationQueue *operationQueue
Discussion

The operation queue on which request operations are scheduled and run.

Declared In

AFHTTPRequestOperationManager.h

reachabilityManager

@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager
Discussion

The network reachability manager. AFHTTPRequestOperationManager uses the sharedManager by default.

Declared In

AFHTTPRequestOperationManager.h

requestSerializer

@property (nonatomic, strong) AFHTTPRequestSerializer<AFURLRequestSerialization> *requestSerializer
Discussion

Requests created with requestWithMethod:URLString:parameters: & multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock: are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of AFHTTPRequestSerializer, which serializes query string parameters for GET, HEAD, and DELETE requests, or otherwise URL-form-encodes HTTP message bodies.

Warning: requestSerializer must not be nil.

Declared In

AFHTTPRequestOperationManager.h

responseSerializer

@property (nonatomic, strong) AFHTTPResponseSerializer<AFURLResponseSerialization> *responseSerializer
Discussion

Responses sent from the server in data tasks created with dataTaskWithRequest:success:failure: and run using the GET / POST / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to a JSON serializer, which serializes data from responses with a application/json MIME type, and falls back to the raw data object. The serializer validates the status code to be in the 2XX range, denoting success. If the response serializer generates an error in -responseObjectForResponse:data:error:, the failure callback of the session task or request operation will be executed; otherwise, the success callback will be executed.

Warning: responseSerializer must not be nil.

Declared In

AFHTTPRequestOperationManager.h

securityPolicy

@property (nonatomic, strong) AFSecurityPolicy *securityPolicy
Discussion

The security policy used by created request operations to evaluate server trust for secure connections. AFHTTPRequestOperationManager uses the defaultPolicy unless otherwise specified.

Declared In

AFHTTPRequestOperationManager.h

shouldUseCredentialStorage

@property (nonatomic, assign) BOOL shouldUseCredentialStorage
Discussion

Whether request operations should consult the credential storage for authenticating the connection. YES by default.

Declared In

AFHTTPRequestOperationManager.h

Class Methods

manager

+ (instancetype)manager
Discussion

Creates and returns an AFHTTPRequestOperationManager object.

Declared In

AFHTTPRequestOperationManager.h

Instance Methods

DELETE:parameters:success:failure:

- (AFHTTPRequestOperation *)DELETE:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a DELETE request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

GET:parameters:success:failure:

- (AFHTTPRequestOperation *)GET:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a GET request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

HEAD:parameters:success:failure:

- (AFHTTPRequestOperation *)HEAD:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a HEAD request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single arguments: the request operation.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

HTTPRequestOperationWithRequest:success:failure:

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates an AFHTTPRequestOperation, and sets the response serializers to that of the HTTP client.

Parameters

request

The request object to be loaded asynchronously during execution of the operation.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments:, the created request operation and the NSError object describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

PATCH:parameters:success:failure:

- (AFHTTPRequestOperation *)PATCH:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a PATCH request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

POST:parameters:constructingBodyWithBlock:success:failure:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a multipart POST request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

block

A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the AFMultipartFormData protocol.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

POST:parameters:success:failure:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a POST request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

PUT:parameters:success:failure:

- (AFHTTPRequestOperation *)PUT:(NSString *)URLString parameters:(id)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
Discussion

Creates and runs an AFHTTPRequestOperation with a PUT request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.

Declared In

AFHTTPRequestOperationManager.h

initWithBaseURL:

- (instancetype)initWithBaseURL:(NSURL *)url
Discussion

Initializes an AFHTTPRequestOperationManager object with the specified base URL.

This is the designated initializer.

Parameters

url

The base URL for the HTTP client.

Return Value

The newly-initialized HTTP client

Declared In

AFHTTPRequestOperationManager.h