為盡量保證AFN的原汁原味和實(shí)用性,但又不至于讓項(xiàng)目對(duì)第三方框架的過(guò)渡依賴,對(duì)AFN進(jìn)行了二次封裝
1.創(chuàng)建一個(gè)繼承自NSObject的類,在 .h 文件中提供 get,post,upload,cancel四種方法
@class QYUploadFile;
@interface QYHttpTool : NSObject
/// get
+ (void)GET:(NSString *)urlString
parameters:(id)parameters
progress:(void (^)(NSProgress* downloadProgress))downloadProgress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure;
/// post
+ (void)POST:(NSString *)urlString
parameters:(id)parameters
progress:(void (^)(NSProgress *uploadProgress))uploadProgress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure;
/// upload
+ (void)upload:(NSString *)urlString
parameters:(id)parameters
uploadFile:(QYUploadFile* )uploadFile
progress:(void (^)(NSProgress* uploadProgress))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure;
/// 取消發(fā)出請(qǐng)求后對(duì)回調(diào)block的操作
+ (void)cancelAllOperations;
在.m文件具體實(shí)現(xiàn)
+ (void)GET:(NSString *)urlString
parameters:(id)parameters
progress:(void (^)(NSProgress* downloadProgress))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure{
QYHttpSessionManager* manager = [QYHttpSessionManager sharedManager];
[manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
if (progress) {
progress(downloadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
+ (void)POST:(NSString *)urlString
parameters:(id)parameters
progress:(void (^)(NSProgress *uploadProgress))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure{
QYHttpSessionManager* manager = [QYHttpSessionManager sharedManager];
[manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
if (progress) {
progress(uploadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
+ (void)upload:(NSString *)urlString
parameters:(id)parameters
uploadFile:(QYUploadFile* )uploadFile
progress:(void (^)(NSProgress* uploadProgress))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError* error))failure{
QYHttpSessionManager* manager = [QYHttpSessionManager sharedManager];
[manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
/**
上傳的文件全部拼接到formData
*
* FileData:要上傳的文件的二進(jìn)制數(shù)據(jù)
* name:上傳參數(shù)名稱
* fileName:上傳到服務(wù)器的文件名稱
* mimeType:文件類型
*/
[formData appendPartWithFileData:uploadFile.data name:uploadFile.paramName fileName:uploadFile.fileName mimeType:uploadFile.mimeType];
} progress:^(NSProgress * _Nonnull uploadProgress) {
if (progress) {
progress(uploadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
+ (void)cancelAllOperations {
[[QYHttpSessionManager sharedManager].operationQueue cancelAllOperations];
}
2.創(chuàng)建一個(gè)繼承自AFHTTPSessionManager類 QYHttpSessionManager,設(shè)置為單例
//單例方法
+(instancetype)sharedManager {
static QYHttpSessionManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [QYHttpSessionManager manager];
// 其它配置設(shè)置
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
});
return manager;
}
3.為防止工具類中上傳文件方法對(duì)AFMultipartFormData的依賴,創(chuàng)建一個(gè)模型類QYUploadFile存儲(chǔ)上傳文件信息,還可以根據(jù) formData的需要向該類增加新的屬性
/**
* 上傳文件的二進(jìn)制數(shù)據(jù)
*/
@property (nonatomic, strong) NSData *data;
/**
* 上傳的參數(shù)名稱
*/
@property (nonatomic, copy) NSString* paramName;
/**
* 上傳到服務(wù)器后的文件名稱
*/
@property (nonatomic, copy) NSString *fileName;
/**
* 上傳文件的類型
*/
@property (nonatomic, copy) NSString *mimeType;