AFN的二次封装单例类

用一个单例类将一些常用的网络请求进行了封装,主要包括post请求 get请求 图片文件上传下载 视频的断点续传等功能。

.h文件

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>


typedef enum{
    requestTypeGET,
    requestTypePOST
}requestType;

@interface NetworkManger : AFHTTPSessionManager

//单例
+ (instancetype)shareNetworkManger;
+(id)allocWithZone:(NSZone *)zone;

//第一次封装请求类型
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *_Nonnull downloadProgress))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *_Nullable task, id _Nullable responseObject))success
                failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *_Nonnull error))failure;

//第二次封装,简化参数,把没有用的参数去掉
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success  failure:(nullable void (^)(NSError *_Nonnull error))failure;

//第三次封装,直接使用类方法,外部调用单例避免重复
+ (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success  failure:(nullable void (^)(NSError *_Nonnull error))failure;
@end

.m文件

#import "NetworkManger.h"

static NetworkManger *instance;

@implementation NetworkManger

+ (instancetype)shareNetworkManger {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{    
         if (instance == nil) {
           instance = [super allocWithZone:zone];
        }
        //  让AFN默认也支持接收text/html文件类型
        instance.responseSerializer.acceptableContentTypes
        = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
    });
    return instance;
}

//MARK: 一次封装的网络工具类
- (void)requestWithType:(requestType)type URLString:(NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure{
    if (type == requestTypeGET) {
        [instance GET:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
    } else if (type == requestTypePOST) {
        [instance POST:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
    }
}

//MARK: 二次封装的网络工具类
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success  failure:(nullable void (^)(NSError *_Nonnull error))failure{
    //(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
    void (^orginalSuccess)(NSURLSessionDataTask * _Nullable, id  _Nullable) = ^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
//        success((NSDictionary *)responseObject);
        success(responseObject);
    };
    //(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
    void (^orginalFailure)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    };
    [self requestWithType:type URLString:URLString parameters:parameters progress:nil success:orginalSuccess failure:orginalFailure];
}

//MARK: 三次封装的网络工具类
+ (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success  failure:(nullable void (^)(NSError *_Nonnull error))failure {
    [[NetworkManger shareNetworkManger] requestWithType:type URLString:URLString parameters:parameters success:success failure:failure];
}

+(id)allocWithZone:(NSZone *)zone{
  return [NetworkManger shareNetworkManger];
}
// 为了严谨,也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone{
    return [NetworkManger shareNetworkManger];
}
-(id)mutableCopyWithZone:(NSZone *)zone{
    return [NetworkManger shareNetworkManger];
}

推荐阅读更多精彩内容