iOS 网络请求(基于AFNetworking的再次封装)
2019-03-04 本文已影响18人
光之盐汽水
说到AFNetworking,应该是现在应用的最广泛的第三方库了,而且封装的非常好了,不少人拿过AFN之后,直接使用了,也没什么问题,最多接口不统一,维护困难一些~
不过最好还是对AFN再进行维护一番比较好。
XZNetWorkTool.h文件的声明:
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
NS_ASSUME_NONNULL_BEGIN
@interface XZNetWorkTool : NSObject
+ (instancetype)shareInstance;
/**
* 发送get请求
*
* @param url 请求路径
* @param parameters 请求参数
* @param header 请求头Header
* @param downloadProgressBlock 网络请求进度
* @param success 请求成功后的回调,参数为id类型
* @param failure 请求失败后的回调
*/
- (NSURLSessionDataTask *)GET:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
- (NSURLSessionDataTask *)GET:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 发送post请求
*
* @param url 请求路径
* @param parameters 请求参数
* @param header 请求头Header
* @param success 请求成功后的回调,参数为id类型
* @param failure 请求失败后的回调
*/
- (NSURLSessionDataTask *)POST:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
- (NSURLSessionDataTask *)POST:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 上传文件
*
* @param url 请求路径
* @param parameters 请求参数
* @param header 请求头Header
* @param formDataBlock 上传文件的信息
* @param success 请求成功后的回调,参数为id类型
* @param failure 请求失败后的回调
*/
- (NSURLSessionDataTask *)upload:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))formDataBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
- (NSURLSessionDataTask *)upload:(NSString *)url
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))formDataBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 下载
*
* @param url 请求路径
* @param parameters 请求参数
* @param header 请求头Header
* @param downloadProgressBlock 下载进度
* @param destination 请求成功后的回调,参数为id类型
* @param completionHandler 请求失败后的回调
*/
- (NSURLSessionDownloadTask *)download:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler;
- (NSURLSessionDownloadTask *)download:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler;
@end
NS_ASSUME_NONNULL_END
XZNetWorkTool.m文件:
#import "XZNetWorkTool.h"
@implementation XZNetWorkTool
static id _instance = nil;
+ (instancetype)shareInstance {
return [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
- (instancetype)init {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager startMonitoring];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
// 未知网络
break;
case AFNetworkReachabilityStatusNotReachable:
// 无法联网
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
// 手机自带网络2G/3G/4G网络
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
// WIFI网络下
break;
default:
break;
}
}];
});
return _instance;
}
/**
* 发送get请求
*/
- (NSURLSessionDataTask *)GET:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
/**
* 可以接受的类型
*/
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
/**
* 请求超时的时间
*/
manager.requestSerializer.timeoutInterval = 30;
/**
* Header 请求头
*/
if (header) {
[header enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[manager.requestSerializer setValue:obj forHTTPHeaderField:key];
}];
}
NSURLSessionDataTask *task = [manager GET:url parameters:parameters progress:downloadProgressBlock success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
return task;
}
- (NSURLSessionDataTask *)GET:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
return [self GET:url parameters:parameters httpHeaderKeyValues:@{} progress:downloadProgressBlock success:success failure:failure];
}
/**
* 发送post请求
*/
- (NSURLSessionDataTask *)POST:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
/**
* 可以接受的类型
*/
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
/**
* 请求超时的时间
*/
manager.requestSerializer.timeoutInterval = 30;
/**
* Header 请求头
*/
if (header) {
[header enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[manager.requestSerializer setValue:obj forHTTPHeaderField:key];
}];
}
NSURLSessionDataTask *task = [manager POST:url parameters:parameters progress:downloadProgressBlock success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
return task;
}
- (NSURLSessionDataTask *)POST:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
return [self POST:url parameters:parameters httpHeaderKeyValues:@{} progress:downloadProgressBlock success:success failure:failure];
}
/**
* 上传文件
*/
- (NSURLSessionDataTask *)upload:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))formDataBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
/**
* 可以接受的类型
*/
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
/**
* 请求超时的时间
*/
manager.requestSerializer.timeoutInterval = 30;
/**
* Header 请求头
*/
if (header) {
[header enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[manager.requestSerializer setValue:obj forHTTPHeaderField:key];
}];
}
NSURLSessionDataTask *task = [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if (formDataBlock) {
formDataBlock(formData);
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
return task;
}
- (NSURLSessionDataTask *)upload:(NSString *)url
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))formDataBlock
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
return [self upload:url parameters:parameters httpHeaderKeyValues:@{} constructingBodyWithBlock:formDataBlock success:success failure:failure];
}
/**
* 下载
*/
- (NSURLSessionDownloadTask *)download:(NSString *)url
parameters:(id)parameters
httpHeaderKeyValues:(NSDictionary *)header
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:downloadProgressBlock destination:destination completionHandler:completionHandler];
[task resume];
return task;
}
- (NSURLSessionDownloadTask *)download:(NSString *)url
parameters:(id)parameters
progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler {
return [self download:url parameters:parameters httpHeaderKeyValues:@{} progress:downloadProgressBlock destination:destination completionHandler:completionHandler];
}
@end