LCHTTPSessionManager
Objective-C
@interface LCHTTPSessionManager
: LCURLSessionManager <NSSecureCoding, NSCopying>
Swift
class LCHTTPSessionManager : LCURLSessionManager, NSSecureCoding, NSCopying
LCHTTPSessionManager
is a subclass of LCURLSessionManager
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 LCHTTPSessionManager
, 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, LCHTTPRequestOperationManager
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:uploadProgress:downloadProgress: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 <LCURLRequestSerialization>
.
Responses received from the server are automatically validated and serialized by the responseSerializers
property, which is an object conforming to <LCURLResponseSerialization>
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.
Warning
Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance.-
The URL used to construct requests from relative paths in methods like
requestWithMethod:URLString:parameters:
, and theGET
/POST
/ et al. convenience methods.Declaration
Objective-C
@property (nonatomic, strong, readonly, nullable) NSURL *baseURL;
Swift
var baseURL: URL? { get }
-
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 ofLCHTTPRequestSerializer
, which serializes query string parameters forGET
,HEAD
, andDELETE
requests, or otherwise URL-form-encodes HTTP message bodies.Warning
requestSerializer
must not benil
.Declaration
Objective-C
@property (nonatomic, strong) LCHTTPRequestSerializer<LCURLRequestSerialization> *_Nonnull requestSerializer;
Swift
var requestSerializer: LCHTTPRequestSerializer & LCURLRequestSerialization { get set }
-
Responses sent from the server in data tasks created with
dataTaskWithRequest:success:failure:
and run using theGET
/POST
/ et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance ofLCJSONResponseSerializer
.Warning
responseSerializer
must not benil
.Declaration
Objective-C
@property (nonatomic, strong) LCHTTPResponseSerializer<LCURLResponseSerialization> *_Nonnull responseSerializer;
Swift
var responseSerializer: LCHTTPResponseSerializer & LCURLResponseSerialization { get set }
-
The security policy used by created session to evaluate server trust for secure connections.
LCURLSessionManager
uses thedefaultPolicy
unless otherwise specified. A security policy configured withLCSSLPinningModePublicKey
orLCSSLPinningModeCertificate
can only be applied on a session manager initialized with a secure base URL (i.e. https). Applying a security policy with pinning enabled on an insecure session manager throws anInvalid Security Policy
exception.Declaration
Objective-C
@property (nonatomic, strong) LCSecurityPolicy *_Nonnull securityPolicy;
Swift
var securityPolicy: LCSecurityPolicy { get set }
-
Creates and returns an
LCHTTPSessionManager
object.Declaration
Objective-C
+ (nonnull instancetype)manager;
-
Initializes an
LCHTTPSessionManager
object with the specified base URL.Declaration
Objective-C
- (nonnull instancetype)initWithBaseURL:(nullable NSURL *)url;
Swift
convenience init(baseURL url: URL?)
Parameters
url
The base URL for the HTTP client.
Return Value
The newly-initialized HTTP client
-
Initializes an
LCHTTPSessionManager
object with the specified base URL.This is the designated initializer.
Declaration
Objective-C
- (nonnull instancetype)initWithBaseURL:(nullable NSURL *)url sessionConfiguration: (nullable NSURLSessionConfiguration *)configuration;
Swift
init(baseURL url: URL?, sessionConfiguration configuration: URLSessionConfiguration?)
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
-
Deprecated
Creates and runs an
NSURLSessionDataTask
with aGET
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) GET:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func get(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.
-
Creates and runs an
NSURLSessionDataTask
with aGET
request.See
-dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) GET:(nonnull NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *_Nonnull))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func get(_ URLString: String, parameters: Any?, progress downloadProgress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
Parameters
URLString
The URL string used to create the request URL.
parameters
The parameters to be encoded according to the client request serializer.
downloadProgress
A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue.
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.
-
Creates and runs an
NSURLSessionDataTask
with aHEAD
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) HEAD:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func head(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.
-
Deprecated
Creates and runs an
NSURLSessionDataTask
with aPOST
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) POST:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func post(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.
-
Creates and runs an
NSURLSessionDataTask
with aPOST
request.See
-dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) POST:(nonnull NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *_Nonnull))uploadProgress success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func post(_ URLString: String, parameters: Any?, progress uploadProgress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
Parameters
URLString
The URL string used to create the request URL.
parameters
The parameters to be encoded according to the client request serializer.
uploadProgress
A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
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.
-
Deprecated
Creates and runs an
NSURLSessionDataTask
with a multipartPOST
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) POST:(nonnull NSString *)URLString parameters:(nullable id)parameters constructingBodyWithBlock: (nullable void (^)(id<LCMultipartFormData> _Nonnull))block success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure: (nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func post(_ URLString: String, parameters: Any?, constructingBodyWith block: ((LCMultipartFormData) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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
LCMultipartFormData
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.
-
Creates and runs an
NSURLSessionDataTask
with a multipartPOST
request.See
-dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) POST:(nonnull NSString *)URLString parameters:(nullable id)parameters constructingBodyWithBlock: (nullable void (^)(id<LCMultipartFormData> _Nonnull))block progress: (nullable void (^)(NSProgress *_Nonnull))uploadProgress success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure: (nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func post(_ URLString: String, parameters: Any?, constructingBodyWith block: ((LCMultipartFormData) -> Void)?, progress uploadProgress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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
LCMultipartFormData
protocol.uploadProgress
A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
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.
-
Creates and runs an
NSURLSessionDataTask
with aPUT
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) PUT:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func put(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.
-
Creates and runs an
NSURLSessionDataTask
with aPATCH
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) PATCH:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func patch(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.
-
Creates and runs an
NSURLSessionDataTask
with aDELETE
request.See
-dataTaskWithRequest:completionHandler:
Declaration
Objective-C
- (nullable NSURLSessionDataTask *) DELETE:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *_Nonnull, id _Nullable))success failure:(nullable void (^)(NSURLSessionDataTask *_Nullable, NSError *_Nonnull))failure;
Swift
func delete(_ URLString: String, parameters: Any?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)? = nil) -> URLSessionDataTask?
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.