iOS - 基于AFN的网络请求封装
2018-01-11 本文已影响107人
SkyMing一C
图片源于网络
基于AFN的网络请求封装
- .h文件
#import <Foundation/Foundation.h>
/** 请求类型的枚举 */
typedef NS_ENUM(NSUInteger, SkyHttpRequestType)
{
/** get请求 */
SkyHttpRequestTypeGet = 0,
/** post请求 */
SkyHttpRequestTypePost
};
/**
http通讯成功的block
@param responseObject 返回的数据
*/
typedef void (^SkyHTTPRequestSuccessBlock)(id responseObject);
/**
http通讯失败后的block
@param error 返回的错误信息
*/
typedef void (^SkyHTTPRequestFailedBlock)(NSError *error);
//超时时间
extern NSInteger const kAFNetworkingTimeoutInterval;
@interface AFNetworkingManager : NSObject
/**
* 网络请求的实例方法
*
* @param type get / post (项目目前只支持这倆中)
* @param urlString 请求的地址
* @param parameters 请求的参数
* @param successBlock 请求成功回调
* @param failureBlock 请求失败回调
*/
+ (void)requestWithType:(SkyHttpRequestType)type
urlString:(NSString *)urlString
parameters:(NSDictionary *)parameters
successBlock:(SkyHTTPRequestSuccessBlock)successBlock
failureBlock:(SkyHTTPRequestFailedBlock)failureBlock;
/**
取消队列
*/
+(void)cancelDataTask;
@end
- .m文件
#import "AFNetworkingManager.h"
#import "AFHTTPSessionManager.h"
NSInteger const kAFNetworkingTimeoutInterval = 10;
@implementation AFNetworkingManager
static AFHTTPSessionManager *aManager;
+ (AFHTTPSessionManager *)sharedAFManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
aManager = [AFHTTPSessionManager manager];
//以下三项manager的属性根据需要进行配置
aManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/xml",@"text/json",@"text/plain",@"text/JavaScript",@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
aManager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置超时时间
aManager.requestSerializer.timeoutInterval = kAFNetworkingTimeoutInterval;
});
return aManager;
}
+ (void)requestWithType:(SkyHttpRequestType)type
urlString:(NSString *)urlString
parameters:(NSDictionary *)parameters
successBlock:(SkyHTTPRequestSuccessBlock)successBlock
failureBlock:(SkyHTTPRequestFailedBlock)failureBlock
{
if (urlString == nil)
{
return;
}
if (@available(iOS 9.0, *)) {
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}else {
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
if (type == SkyHttpRequestTypeGet)
{
[[self sharedAFManager] GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (successBlock)
{
successBlock(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error.code !=-999) {
if (failureBlock)
{
failureBlock(error);
}
}
else{
NSLog(@"取消队列了");
}
}];
}
if (type == SkyHttpRequestTypePost)
{
[[self sharedAFManager] POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (successBlock)
{
successBlock(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error.code !=-999) {
if (failureBlock)
{
failureBlock(error);
}
}
else{
NSLog(@"取消队列了");
}
}];
}
}
+(void)cancelDataTask
{
NSMutableArray *dataTasks = [NSMutableArray arrayWithArray:[self sharedAFManager].dataTasks];
for (NSURLSessionDataTask *taskObj in dataTasks) {
[taskObj cancel];
}
}
@end
主要步骤
-
创建的AFNetworkingManager类继承自NSObject类
-
请求类型用枚举定义
/** 请求类型的枚举 */
typedef NS_ENUM(NSUInteger, SkyHttpRequestType)
{
/** get请求 */
SkyHttpRequestTypeGet = 0,
/** post请求 */
SkyHttpRequestTypePost
};
- 用block回调请求结果
/**
http通讯成功的block
@param responseObject 返回的数据
*/
typedef void (^SkyHTTPRequestSuccessBlock)(id responseObject);
/**
http通讯失败后的block
@param error 返回的错误信息
*/
typedef void (^SkyHTTPRequestFailedBlock)(NSError *error);
- 在.m文件中导入#import "AFHTTPSessionManager.h",并获取AFHTTPSessionManager类単例
static AFHTTPSessionManager *aManager;
+ (AFHTTPSessionManager *)sharedAFManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
aManager = [AFHTTPSessionManager manager];
//以下三项manager的属性根据需要进行配置
aManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/xml",@"text/json",@"text/plain",@"text/JavaScript",@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
aManager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置超时时间
aManager.requestSerializer.timeoutInterval = kAFNetworkingTimeoutInterval;
});
return aManager;
}
- failure返回的"error.code == -999"为取消请求队列
if (error.code !=-999) {
if (failureBlock)
{
failureBlock(error);
}
}
else{
NSLog(@"取消队列了");
}
- 以上