AFNetworking的封装(三)--OC黑魔法
2016-08-26 本文已影响1686人
js_huh
- 采用AFN自带的私有方法,对GET和POST进行封装!
- 在AFN的AFHTTPSessionManager.m里有dataTaskWithHTTPMethod私有方法! 发出的GET和POST请求,都要经过它! 所以,直接调用此方法就可以实现对请求的封装!
- dataTaskWithHTTPMethod是个私有方法,XCode不允许调用!那么,可以通过,先自定义协议,协议里面对dataTaskWithHTTPMethod方法进行声明,但并没有实现此方法 ,那么,XCode会去调用父类的dataTaskWithHTTPMethod方法!(也就是AFN的dataTaskWithHTTPMethod方法)这样就绕过了XCode,实现了AFN私有的方法!
注意: 封装的请求方法,requestProxy 后面要调用resume,因为是调用AFN的dataTaskWithHTTPMethod方法,此方法,后面有resume方法
#import "NetworkTools.h"
@protocol NetwookToolsProxy <NSObject>
/// 网络请求方法
///
/// @param method HTTP 请求方法
/// @param URLString URLString
/// @param parameters 参数字典
/// @param uploadProgress 上传进度
/// @param downloadProgress 下载进度
/// @param success 成功回调
/// @param failure 失败回调
///
/// @return NSURLSessionDataTask
/// 调用的是AFHTTPSessionManager.m --> dataTaskWithHTTPMethod 方法
@optional
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
@end
/// 遵守网络协议 - 为了能够欺骗 Xcode 给一个智能提示,以及编译通过!
@interface NetworkTools () <NetwookToolsProxy>
@end
@implementation NetworkTools
/// NetworkTools 的单例方法
+(instancetype)sharedTools{
static NetworkTools *tools;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 注意:末尾需要包含 '/'
NSURL *baseURL = [NSURL URLWithString:@"http://www.weather.com.cn/"];
tools = [[NetworkTools alloc]initWithBaseURL:baseURL];
// 设置反序列化格式
tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
});
return tools;
}
///通过AFN已经封装好的方法,来实现GET和POST方法
/// 注意:resume
-(void)requestProxy:(RequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(void(^)(id result, NSError *error))finished {
NSString *methodName = (method == GET) ? @"GET": @"POST";
[[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
finished(responseObject,nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
finished(nil,error);
}]resume];
}
//调用
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self afnRequestProxy];
}
/// 已经封装的AFN请求(使用AFN自带的私有方法,通过协议,绕过Xcode,来实现POST和GET请求)
-(void)afnRequestProxy{
NetworkTools *tools = [NetworkTools sharedTools];
/*
GET URLString:@"data/sk/101010100.html" parameters:nil
POST URLString::@"http://httpbin.org/post" parameters:@{@"name": @"zhangsan", @"age": @18}
*/
[tools requestProxy:POST URLString:@"http://httpbin.org/get" parameters:@{@"name": @"zhangsan", @"age": @18} finished:^(id result, NSError *error) {
if(result != nil){
NSLog(@"调用成功了 %@",result);
}else if(error != nil){
NSLog(@"调用失败了:%@",error);
}
}];
}