博客> 对AFNetworking再封装
对AFNetworking再封装
2017-12-14 11:39 评论:0 阅读:536 CuiZhanQi
ios AFN

/**

  • 图片的二进制数据 / @property (nonatomic, strong) NSData data; /**
  • 服务器对应的参数名称 / @property (nonatomic, copy) NSString name; /**
  • 文件的名称(上传到服务器后,服务器保存的文件名) / @property (nonatomic, copy) NSString filename; /**
  • 文件的MIME类型(image/png,image/jpg等) / @property (nonatomic, copy) NSString mimeType;

@end

继续创建一个类 HttpRequest.h的申明:

复制代码 @class UploadParam; /**

  • 网络请求类型 */ typedef NS_ENUM(NSUInteger,HttpRequestType) { /**
    • get请求 */ HttpRequestTypeGet = 0, /**
    • post请求 */ HttpRequestTypePost };

@interface HttpRequest : NSObject

  • (instancetype)sharedInstance;

/**

  • 发送get请求
  • @param URLString 请求的网址字符串
  • @param parameters 请求的参数
  • @param success 请求成功的回调
  • @param failure 请求失败的回调 */
    • (void)getWithURLString:(NSString )URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError error))failure;

/**

  • 发送post请求
  • @param URLString 请求的网址字符串
  • @param parameters 请求的参数
  • @param success 请求成功的回调
  • @param failure 请求失败的回调 */
    • (void)postWithURLString:(NSString )URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError error))failure;

/**

  • 发送网络请求
  • @param URLString 请求的网址字符串
  • @param parameters 请求的参数
  • @param type 请求的类型
  • @param resultBlock 请求的结果 */
    • (void)requestWithURLString:(NSString )URLString parameters:(id)parameters type:(HttpRequestType)type success:(void (^)(id responseObject))success failure:(void (^)(NSError error))failure;

/**

  • 上传图片
  • @param URLString 上传图片的网址字符串
  • @param parameters 上传图片的参数
  • @param uploadParam 上传图片的信息
  • @param success 上传成功的回调
  • @param failure 上传失败的回调 */
    • (void)uploadWithURLString:(NSString )URLString parameters:(id)parameters uploadParam:(NSArray )uploadParams success:(void (^)())success failure:(void (^)(NSError *error))failure;

/**

  • 下载数据
  • @param URLString 下载数据的网址
  • @param parameters 下载数据的参数
  • @param success 下载成功的回调
  • @param failure 下载失败的回调 */
    • (void)downLoadWithURLString:(NSString )URLString parameters:(id)parameters progerss:(void (^)())progress success:(void (^)())success failure:(void (^)(NSError error))failure;

@end

类的.m实现文件

@implementation HttpRequest

static id _instance = nil;

  • (instancetype)sharedInstance { 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: { // 位置网络 NSLog(@"位置网络"); } break; case AFNetworkReachabilityStatusNotReachable: { // 无法联网 NSLog(@"无法联网"); } break; case AFNetworkReachabilityStatusReachableViaWiFi: { // 手机自带网络 NSLog(@"当前使用的是2G/3G/4G网络"); } break; case AFNetworkReachabilityStatusReachableViaWWAN: { // WIFI NSLog(@"当前在WIFI网络下"); } } }]; }); return _instance; }

pragma mark -- GET请求 --

  • (void)getWithURLString:(NSString )URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError ))failure {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; /**

    • 可以接受的类型 */ manager.responseSerializer = [AFHTTPResponseSerializer serializer]; /**
    • 请求队列的最大并发数 */ // manager.operationQueue.maxConcurrentOperationCount = 5; /**
    • 请求超时的时间 / manager.requestSerializer.timeoutInterval = 30; [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask _Nullable task, NSError _Nonnull error) { if (failure) { failure(error); } }]; }

pragma mark -- POST请求 --

  • (void)postWithURLString:(NSString )URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError ))failure {

    AFHTTPSessionManager manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask _Nullable task, NSError _Nonnull error) { if (failure) { failure(error); } }]; }

pragma mark -- POST/GET网络请求 --

  • (void)requestWithURLString:(NSString )URLString parameters:(id)parameters type:(HttpRequestType)type success:(void (^)(id))success failure:(void (^)(NSError ))failure {

    AFHTTPSessionManager manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; switch (type) { case HttpRequestTypeGet: { [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask _Nullable task, NSError _Nonnull error) { if (failure) { failure(error); } }]; } break; case HttpRequestTypePost: { [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; } break; } }

  • (void)uploadWithURLString:(NSString )URLString parameters:(id)parameters uploadParam:(NSArray )uploadParams success:(void (^)())success failure:(void (^)(NSError ))failure { AFHTTPSessionManager manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id _Nonnull formData) { for (UploadParam uploadParam in uploadParams) { [formData appendPartWithFileData:uploadParam.data name:uploadParam.name fileName:uploadParam.filename mimeType:uploadParam.mimeType]; } } progress:nil success:^(NSURLSessionDataTask _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask _Nullable task, NSError _Nonnull error) { if (failure) { failure(error); } }]; }

pragma mark - 下载数据

  • (void)downLoadWithURLString:(NSString )URLString parameters:(id)parameters progerss:(void (^)())progress success:(void (^)())success failure:(void (^)(NSError ))failure { AFHTTPSessionManager manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSURLRequest request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]]; NSURLSessionDownloadTask downLoadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress _Nonnull downloadProgress) { if (progress) { progress(); } } destination:^NSURL _Nonnull(NSURL _Nonnull targetPath, NSURLResponse _Nonnull response) { return targetPath; } completionHandler:^(NSURLResponse _Nonnull response, NSURL _Nullable filePath, NSError _Nullable error) { if (failure) { failure(error); } }]; [downLoadTask resume]; }

@end

收藏
1
sina weixin mail 回到顶部