AFNetWorking3.0的学习

AFNetWorking3.0和2.x相比:

1.移除了三个类:

AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager

2.把基于NSURLConnection的类进行了重构,完全基于NSURLSession:

UIImageView+AFNetworking
UIWebView+AFNetworking.h
UIButton+AFNetworking.h

3.之前使用的AFHTTPRequestOperationManager现在需要迁移到AFHTTPSessionManager,其中下面这部分类保留了复用:

securityPolicy
requestSerializer
responseSerializer

而且请求方法返回的是NSURLSessionTask,而不再是AFHTTPRequestOperation,同时成功失败的回调也是NSURLSessionTask,也不再是AFHTTPRequestOperation;

一个例子:

AFNetworking 2.x

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

AFNetworking 3.x

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

4.AFHTTPRequestOperation 核心代码:

和NSURLConnection不同,每个共享应用范围的设置如,会话管理,缓存策略,cookie存储,以及URL协议等,这些NSURLSession对象都可以单独配置。使用特定的配置来初始化会话,它可以发送任务来获取数据,并上传或下载文件。

在AFNetWorking2.x中,使用AFHTTPRequestOperation,可以创建一个没有额外开销的独立的网络请求来获取数据。但是,NSURLSession需要更多的开销,为了获得所要请求的数据。

接下来,用AFHTTPSessionManager创建一个对象,并创建一个任务和启动它:

AFNetworking 2.x

NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];

AFNetworking 3.x

NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

请求头的设置:

AFHTTPSessionManager  *_sessionManager = [AFHTTPSessionManager manager];
_sessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
_sessionManager.requestSerializer.timeoutInterval = 10;
_sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
_sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
[_sessionManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[_sessionManager.requestSerializer setValue:[self getHelpToken] forHTTPHeaderField:@"Authorization"];

请求超时和接受类型:

manager.requestSerializer.timeoutInterval = 10;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", @"text/plain", nil];

UIKit的迁移:

  • 图片下载被重构,以遵循AlamofireImage架构和AFImageDownloader新的类,这个类的下载职责的代理人是UIButton和UIImageView的类目,而且提供了一些方法,必要时可自定义。类别中下载远程图片的实际方法没有改变。

  • UIWebview的类被重构为使用AFHTTPSessionManager作为其网络请求。

  • 从AFNetworking3.0开始UIAlertview的类因为过时被废弃了。也没有提供UIAlertController类的计划,因为这是应用程序应该处理的逻辑,而不是这个库。

你可能感兴趣的:(AFNetWorking3.0的学习)