封装的AFNetWork工具.m

@implementation ZBJAFNetworkTool

+ (void)getUrl:(NSString *)url

body:(id)body

result:(ZBJResult)result

headerFile:(NSDictionary *)headerFile

success:(void (^)(id result))success

failure:(void (^)(NSError *error))failure

{

NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];

NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];

id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:caches];

if (responseObject != nil) {

success(responseObject);

}

/**

*  1.获取网络请求管理类

*/

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//    3.为网络请求加请求头

if (headerFile) {

for (NSString *key in headerFile.allKeys) {

[manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];

}

}

//    4.设置返回值类型

switch (result) {

/**

*  返回值为NSData

*/

case ZBJData:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

break;

case ZBJJson:

break;

case ZBJXML:

/**

*  XML型

*/

manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

break;

default:

break;

}

//    2.设置网络请求返回值所支持的参数类型

[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];

#pragma mark 让网络指示器旋转

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

//    5.发送网络请求

[manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {

if (responseObject) {

//            成功回调

success(responseObject);

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

//            NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];

// 缓存

NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

NSString *sandBoxPath = sandBox[0];

NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];

[NSKeyedArchiver archiveRootObject:responseObject toFile:caches];

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if (error) {

//            失败回调

failure(error);

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

}];

}

+ (void)postUrl:(NSString *)url

body:(id)body

result:(ZBJResult)result

requestStyle:(ZBJRequestStyle)requestStyle

headerFile:(NSDictionary *)headerFile

success:(void (^)(id result))success

failure:(void (^)(NSError *error))failure

{

//    1.获取网络请求管理类

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//    3.网络请求请求体的类型

switch (requestStyle) {

case ZBJRequestJSON:

manager.requestSerializer = [AFJSONRequestSerializer serializer];

break;

case ZBJRequestString:

[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {

return parameters;

}];

break;

default:

break;

}

//    4.为网络请求加请求头

if (headerFile) {

for (NSString *key in headerFile.allKeys) {

[manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];

}

}

//    5.网络请求返回值的数据类型

switch (result) {

/**

*  返回值为NSData

*/

case ZBJData:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

break;

case ZBJJson:

break;

case ZBJXML:

/**

*  XML型

*/

manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

break;

default:

break;

}

//    2.设置网络请求返回值支持的类型

[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];

//    6.发送网络请求

[manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {

if (responseObject) {

success(responseObject);

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if (error) {

failure(error);

}

}];

}

你可能感兴趣的:(封装的AFNetWork工具.m)