Courier是一个基于NSURLSession和各种便捷类的简单网络层。
#import <Courier/Courier.h>
// Default config
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
// Add additional headers
config.HTTPAdditionalHeaders = @{@"Accept" : @"application/json"};
// Be a good API citizen and limit to 1 HTTP connection per host
config.HTTPMaximumConnectionsPerHost = 1;
// Build session controller
CRSessionController *sessionController = [CRSessionController sessionControllerWithConfiguration:config
delegate:controller];
// Build a POST request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithMethod:@"POST"
path:@"https://service.com/api"
encoding:CR_URLJSONParameterEncoding
URLParameters:@{@"urlParam" : @"value"}
HTTPBodyParameters:@{@"bodyParam" : @"value"}
header:@{@"Header-Name" : @"value"}];
// Build a task
NSURLSessionDataTask *task = [sessionController dataTaskForRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (response.success) {
// Hurrah!
}
}];
// Start task
[task resume];