AFHTTPSessionManager Class Reference
Inherits from | AFURLSessionManager : NSObject |
Conforms to | NSCoding NSCopying |
Declared in | AFHTTPSessionManager.h AFHTTPSessionManager.m |
Overview
AFHTTPSessionManager
is a subclass of AFURLSessionManager
with convenience methods for making HTTP requests. When a baseURL
is provided, requests made with the GET
/ POST
/ et al. convenience methods can be made with relative paths.
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 data task operation construction, which is also used in the GET
/ POST
/ et al. convenience methods, override dataTaskWithRequest:completionHandler:
.
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.
Tasks
Other Methods
-
baseURL
property -
requestSerializer
property -
responseSerializer
property
Initialization
Making HTTP Requests
-
– GET:parameters:success:failure:
-
– HEAD:parameters:success:failure:
-
– POST:parameters:success:failure:
-
– POST:parameters:constructingBodyWithBlock:success:failure:
-
– PUT:parameters:success:failure:
-
– PATCH:parameters:success:failure:
-
– DELETE:parameters:success:failure:
Other Methods
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
AFHTTPSessionManager.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
AFHTTPSessionManager.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 an instance of AFJSONResponseSerializer
.
@property (nonatomic, strong) AFHTTPResponseSerializer<AFURLResponseSerialization> *responseSerializer
Discussion
Warning: responseSerializer
must not be nil
.
Declared In
AFHTTPSessionManager.h
Instance Methods
DELETE:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a DELETE
request.
- (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
GET:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a GET
request.
- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
HEAD:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a HEAD
request.
- (NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes a single arguments: the data task.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
PATCH:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a PATCH
request.
- (NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
POST:parameters:constructingBodyWithBlock:success:failure:
Creates and runs an NSURLSessionDataTask
with a multipart POST
request.
- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
POST:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a POST
request.
- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
PUT:parameters:success:failure:
Creates and runs an NSURLSessionDataTask
with a PUT
request.
- (NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(void ( ^ ) ( NSURLSessionDataTask *task , 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 task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
- failure
A block object to be executed when the task 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 data task and the error describing the network or parsing error that occurred.
Declared In
AFHTTPSessionManager.h
initWithBaseURL:
Initializes an AFHTTPSessionManager
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
Declared In
AFHTTPSessionManager.h
initWithBaseURL:sessionConfiguration:
Initializes an AFHTTPSessionManager
object with the specified base URL.
- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration
Parameters
- url
The base URL for the HTTP client.
- configuration
The configuration used to create the managed session.
Return Value
The newly-initialized HTTP client
Discussion
This is the designated initializer.
Declared In
AFHTTPSessionManager.h
initWithSessionConfiguration:
Creates and returns a manager for a session created with the specified configuration. This is the designated initializer.
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration
Parameters
- configuration
The configuration used to create the managed session.
Return Value
A manager for a newly-created session.
Declared In
AFURLSessionManager.h