话说苹果的东西封闭就算了,还是强制性的,今天刚上班,群里不停有人发这鬼东西:
我当时真想对苹果竖起我的中指,然后说一句正宗的美式腔调的英语:are you fucking kidding me!! 不过估计到时候不进行代码迁移的话估计就不能兼容IPV6-only标准了,然后就不能上架appstore.可怜我还在用着NSURLConnection的废弃api,没办法了,只能迁移了,可是有不少同学可能跟我处于一个比较尴尬的境地,如果我们只是单纯的自己对AF进行封装,只要升级AF到3.0以上,就可以满足苹果的要求了,但是如果我们用来一些对AF二次封装的框架,就显得比较麻烦了,例如有的同学会用唐巧的猿题库网络框架,这些框架都是没有迁移到AF3.0的,这篇文章,我会以一个GJNetWorking的库讲解一下我的迁移经历.
我们来对比一下2.x和3.x都发生了什么变化:
由此可见:被苹果官方废弃的NSURLConnection已经被AF删掉了.所以我们应该面向AFHTTPSessionManager编程 而非AFHTTPRequestOperationManager.
介绍一下GJNetWorking,当时使用它来进行项目开发 主要是因为代码比较简洁,支持网络缓存,支持mjestension和mantle的模型映射.我们进行升级AF,主要是对GJHTTPManager GJBaseRequest GJModelRequest这几个类进行代码修改.在GJHTTPManager中使用AFHTTPSessionManager替换掉原来的AFHTTPRequestOperationManager
//原来的AFHTTPRequestOperationManager
AFHTTPRequestOperation *startOperation = nil;
switch (method) {
case GJRequestGET:
{
startOperation = [self.manager GET:url
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self requestFinishedWithOperation:operation request:request];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self requestFinishedWithOperation:operation request:request];
}];
}
break;
//修改后的的AFHTTPSessionManager
NSURLSessionDataTask *startOperation = nil;
switch (method) {
case GJRequestGET:
{
startOperation = [self.manager GET:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self requestFinishedWithOperation:task request:request resonseObj:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self requestFinishedWithOperation:task request:request resonseObj:nil];
}];
}
break;
不过比较难的是响应数据的处理,由于在2.0时代作者使用AFHTTPRequestOperation作为返回的结果处理缓存 和模型映射,3.0中这已经被删除了,所以我们只能换一种思路:直接处理从AFHTTPSessionManager
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure```
方法中返回的responseObject.
GJBaseRequest中增加方法实现
- (void)requestCompletedWithModel:(id)model;
GJModelRequest中修改方法实现
-
(void)requestCompletedWithObj:(id)obj andModelObjBlock:(void (^)(id model))modelBlock{
[super requestCompletedWithObj:obj andModelObjBlock:modelBlock];
BOOL success = !self.error;
id responseStatus;
id responseObject = obj;//if request success and request implement modelClass,
//when request or default modelMaker implement the delegate ,
//the response object will be make to model or model list.
if (success && [self respondsToSelector:@selector(modelClass)]){
Class defaultModelMaker = [GJNetworkingConfig modelMaker];
Class modelMaker = nil;
if (self && [[self class] respondsToSelector:@selector(makeModelWithJSON:class:status:)] &&
[[self class] conformsToProtocol:@protocol(GJModelMakerDelegate)]) {
modelMaker = [self class];
}
else if (defaultModelMaker && [defaultModelMaker respondsToSelector:@selector(makeModelWithJSON:class:status:)] &&
[defaultModelMaker conformsToProtocol:@protocol(GJModelMakerDelegate)]){
modelMaker = defaultModelMaker;
}if (modelMaker) { id model = [modelMaker makeModelWithJSON:responseObject class:[self modelClass] status:&responseStatus]; modelBlock(model); self.status = responseStatus; }
}
}
好了大公告成:
![](http://upload-images.jianshu.io/upload_images/1145379-62f7c4feaf7b0625.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/1145379-4d3dd42f73f4e311.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
good boy!