WRNetwork 是基于 AFNetworking 封装的一套网络请求框架。
- 支持 文件上传和下载
- 支持 GET / POST / DELETE / PUT / HEAD / PATCH 六种请求方式
- 支持 block 和 代理 两种回调请求方式
- 支持 网络请求结果缓存
- 支持 分页请求数据缓存
- 支持 添加缓存过期时间
- 支持 批量请求数据
推荐使用方式请看文件 WRApis.h/m
发起网络请求(这里会立即发起请求)
self.reqWXList = req_wx_list(_curPage, self);
请求 成功 / 失败 / 获取缓存 回调
#pragma mark - WRNetWrapperRequestDelegate
- (void)netWrapperRequestDidSuccess:(WRNetWrapper *)netWrapper {
if (netWrapper == _reqWXList) {
NSDictionary *dict = netWrapper.data;
[self handleResponse:dict];
_reqWXList = nil;
}
}
- (void)netWrapperRequestDidFailed:(WRNetWrapper *)netWrapper {
if (netWrapper == _reqWXList) {
[self.tableView.mj_footer endRefreshing];
_reqWXList = nil;
}
}
- (void)netWrapperGetCacheDidFinished:(WRNetWrapper *)netWrapper {
NSDictionary *dict = netWrapper.cache;
[self handleResponse:dict];
}
批量网络请求使用方式
// ⚠️ 需要注意,这里不会立即发起请求(实现方式请看WRApis.m)
_reqTestA = req_test_a(self);
_reqTestB = req_test_b(self);
_reqTestC = req_test_c(self);
_reqTestD = req_test_d(self);
_reqTestE = req_test_e(self);
_reqTestF = req_test_f(self);
// 生成多请求对象
_multiReq = [[WRMultiRequest alloc] initWithRequestArray:@[_reqTestA,_reqTestB,_reqTestC,_reqTestD,_reqTestE,_reqTestF]
delegate:self];
// 立即发起请求
[_multiReq loadRequests];
批量请求 成功 / 失败 回调
#pragma mark - WRMultiRequestDelegate
- (void)multiRequestDidSuccess:(WRMultiRequest *)multiRequest {
NSLog(@"批量请求全部完成~");
}
- (void)multiRequestDidFailed:(WRMultiRequest *)multiRequest {
NSLog(@"批量请求出错~");
}
当然你也可以使用 block 的方式
- (WRNetWrapper *)requestWithURL:(NSString *)urlStr
parameters:(NSDictionary *)parameters
httpMethod:(WRNetWrapperHTTPMethod)httpMethod
isCache:(BOOL)isCache
cacheTime:(NSTimeInterval)cacheTime
cache:(WRNetWrapperGetCacheBlock)cache
success:(WRNetWrapperRequestDidSuccessBlock)success
failure:(WRNetWrapperRequestDidFailedBlock)failure;
上传文件
+ (__kindof NSURLSessionTask *)uploadWithURL:(NSString *)urlStr
parameters:(id)parameters
name:(NSString *)name
filePath:(NSString *)filePath
progress:(WRNetWrapperProgress)progress
success:(WRNetWrapperRequestDidSuccessBlock)success
failure:(WRNetWrapperRequestDidFailedBlock)failure;
下载文件
+ (__kindof NSURLSessionTask *)downloadWithURL:(NSString *)urlStr
fileDir:(NSString *)fileDir
progress:(WRNetWrapperProgress)progress
success:(void(^)(NSString *filePath))success
failure:(WRNetWrapperRequestDidFailedBlock)failure;
将 WRNetwork 文件夹拽入项目中,导入头文件:#import "WRNetwork.h"
WRNetwork 被许可在 MIT 协议下使用