1. the project uses AFNetworking, which has been encapsulated as a tool class. The new version of 20180911 has been updated, and a preliminary study has been made on this
1.1 use case list:
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
NSMutableSet *acceptableSet = [NSMutableSet setWithSet:mgr.responseSerializer.acceptableContentTypes];
[acceptableSet addObject:@"text/html"];
AFJSONResponseSerializer *jsonResponse = [AFJSONResponseSerializer serializer];
jsonResponse.removesKeysWithNullValues = YES;
jsonResponse.acceptableContentTypes = acceptableSet;
mgr.responseSerializer = jsonResponse;
// 2. Send request
[mgr POST:url parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
ResultStatus result = 0;
if ([responseObject[@"result"] isKindOfClass:[NSString class]]) { // if result is NSString
result = empty(responseObject[@"result"]) ? 0 : [responseObject[@"result"] integerValue];
} else if ([responseObject[@"result"] isKindOfClass:[NSNumber class]]) { // if result is NSNumber
result = [responseObject[@"result"] intValue] == 1 ? ResultStatusSuccess : ResultStatusFailure;
}
success(result, responseObject);
}
// Delete circular reference
[mgr invalidateSessionCancelingTasks:YES];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
// Delete circular reference
[mgr invalidateSessionCancelingTasks:YES];
}];
2. Management Manager:
2.1 inheritance relationship
AFHTTPSessionManager : AFURLSessionManager : NSObject
2.2 implementation of agent method:
<NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying>
2.3 it mainly uses the AFHTTPSessionManager,
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager mainly creates a series of default configurations, including thread, request configuration, detail configuration, security configuration, connection configuration, lock and NSURLSession
if (!configuration) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
}
self.sessionConfiguration = configuration;
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.securityPolicy = [AFSecurityPolicy defaultPolicy];
#if !TARGET_OS_WATCH
self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
#endif
self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];
self.lock = [[NSLock alloc] init];
self.lock.name = AFURLSessionManagerLockName;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDataTask *task in dataTasks) {
[self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
}
for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
[self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
}
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
}
}];
2.4 configurable manager requestSerializer and responseSerializer
2.5 manage can directly call post get request method to get data through block
- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(nullable id)parameters
headers:(nullable NSDictionary <NSString *, NSString *> *)headers
progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(nullable id)parameters
headers:(nullable NSDictionary <NSString *, NSString *> *)headers
progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
Use NSMutableURLRequest for post request
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
post request return data processing
__block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:uploadProgress completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(task, error);
}
} else {
if (success) {
success(task, responseObject);
}
}
}];