AFHTTPRequestOperationManager Class Reference
Inherits from | NSObject |
Conforms to | NSCoding NSCopying |
Declared in | AFHTTPRequestOperationManager.h AFHTTPRequestOperationManager.m |
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.
NSCoding & NSCopying Caveats
AFHTTPRequestOperationManager
conforms to the NSCoding
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.
- NSCoding 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
property -
requestSerializer
property -
responseSerializer
property -
operationQueue
property
Managing URL Credentials
-
shouldUseCredentialStorage
property -
credential
property
Managing Security Policy
-
securityPolicy
property
Managing Network Reachability
-
reachabilityManager
property
Creating and Initializing HTTP Clients
Managing HTTP Request Operations
Making HTTP Requests
Properties
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 (readonly, nonatomic, strong) NSURL *baseURL
Declared In
AFHTTPRequestOperationManager.h
credential
The credential used by request operations for authentication challenges.
@property (nonatomic, strong) NSURLCredential *credential
Declared In
AFHTTPRequestOperationManager.h
operationQueue
The operation queue on which request operations are scheduled and run.
@property (nonatomic, strong) NSOperationQueue *operationQueue
Declared In
AFHTTPRequestOperationManager.h
reachabilityManager
The network reachability manager. AFHTTPRequestOperationManager
uses the sharedManager
by default.
@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager
Declared In
AFHTTPRequestOperationManager.h
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 (nonatomic, strong) AFHTTPRequestSerializer<AFURLRequestSerialization> *requestSerializer
Discussion
Warning: requestSerializer
must not be nil
.
Declared In
AFHTTPRequestOperationManager.h
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 (nonatomic, strong) AFHTTPResponseSerializer<AFURLResponseSerialization> *responseSerializer
Discussion
Warning: responseSerializer
must not be nil
.
Declared In
AFHTTPRequestOperationManager.h
securityPolicy
The security policy used by created request operations to evaluate server trust for secure connections. AFHTTPRequestOperationManager
uses the defaultPolicy
unless otherwise specified.
@property (nonatomic, strong) AFSecurityPolicy *securityPolicy
Declared In
AFHTTPRequestOperationManager.h
shouldUseCredentialStorage
Whether request operations should consult the credential storage for authenticating the connection. YES
by default.
@property (nonatomic, assign) BOOL shouldUseCredentialStorage
Declared In
AFHTTPRequestOperationManager.h
Instance Methods
DELETE:parameters:success:failure:
Creates and runs an AFHTTPRequestOperation
with a DELETE
request.
- (AFHTTPRequestOperation *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a GET
request.
- (AFHTTPRequestOperation *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a HEAD
request.
- (AFHTTPRequestOperation *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates an AFHTTPRequestOperation
, and sets the response serializers to that of the HTTP client.
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a PATCH
request.
- (AFHTTPRequestOperation *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a multipart POST
request.
- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a POST
request.
- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Creates and runs an AFHTTPRequestOperation
with a PUT
request.
- (AFHTTPRequestOperation *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure
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:
Initializes an AFHTTPRequestOperationManager
object with the specified base URL.
- (instancetype)initWithBaseURL:(NSURL *)url
Parameters
- url
The base URL for the HTTP client.
Return Value
The newly-initialized HTTP client
Discussion
This is the designated initializer.
Declared In
AFHTTPRequestOperationManager.h