项目中用了 YTKNetWork,服务器返回的json 数据,但是content-Type 是text/html (吐槽就不说了,不能惯着他们),解决问题才是关键;
首先查找YTK接口:
YTKBaseRequest 类中提供:
-(YTKResponseSerializerType)responseSerializerType;
来看枚举: YTKResponseSerializerType 并没有html,怎么办? 找 AFHTTPSessionManager 在哪呗;
/// Response serializer type, which determines response serialization process and
/// the type of `responseObject`.
typedef NS_ENUM(NSInteger, YTKResponseSerializerType) {
/// NSData type
YTKResponseSerializerTypeHTTP,
/// JSON object type
YTKResponseSerializerTypeJSON,
/// NSXMLParser type
YTKResponseSerializerTypeXMLParser,
};
YTKNetworkAgent 类
在YTKNetworkAgent 发现有如下片段代码
@implementation YTKNetworkAgent {
AFHTTPSessionManager *_manager;
...
...
}
- (instancetype)init {
self = [super init];
if (self) {
_config = [YTKNetworkConfig sharedConfig];
_manager = [AFHTTPSessionManager manager];
_requestsRecord = [NSMutableDictionary dictionary];
_processingQueue = dispatch_queue_create("com.yuantiku.networkagent.processing", DISPATCH_QUEUE_CONCURRENT);
_allStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(100, 500)];
pthread_mutex_init(&_lock, NULL);
_manager.securityPolicy = _config.securityPolicy;
_manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// Take over the status code validation
_manager.responseSerializer.acceptableStatusCodes = _allStatusCodes;
_manager.completionQueue = _processingQueue;
}
return self;
}
runtime 动态修改属性
找到了AFHTTPSessionManager 就好办了,自然会想到拦截 YTKNetworkAgent 的init 方法,动态修改content-type,实现如下:
//
// YTKNetworkAgent+dt_Agent.m
// grid
//
// Created by detao on 16/12/2.
// Copyright © 2016年 德稻全球创新网络(北京)有限公司. All rights reserved.
//
//==============================================================================
// function:扩展 YTKNetWork acceptableContentTypes,只是提供类型支持
//==============================================================================
#import "objc/runtime.h"
@implementation YTKNetworkAgent (dt_Agent)
+(void)load{
// 实现init 与 dt_init方法的交换
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL org_Selector = @selector(init);
SEL dt_Selector = @selector(dt_init);
Method org_method = class_getInstanceMethod([self class], org_Selector);
Method dt_method = class_getInstanceMethod([self class], dt_Selector);
BOOL isAdd = class_addMethod(self, org_Selector, method_getImplementation(dt_method), method_getTypeEncoding(dt_method));
if (isAdd) {
class_replaceMethod(self, dt_Selector, method_getImplementation(org_method), method_getTypeEncoding(org_method));
}else{
method_exchangeImplementations(org_method, dt_method);
}
});
}
-(instancetype)dt_init{
[self dt_init];
[self dt_class_copyIvarList:[self class]];
return self;
}
- (void)dt_class_copyIvarList:(Class)class {
unsigned int count;
Ivar *ivarList = class_copyIvarList(class, &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivarList[i];
// 获取成员属性名
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
// NSString *value = object_getIvar(obj, ivar);
if ([name isEqualToString:@"_manager"]) {
AFHTTPSessionManager *dt_manager = [AFHTTPSessionManager manager];
dt_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"application/json",nil];
// 修改成员变量的值
object_setIvar(self, ivar,dt_manager);
}
}
// 记得释放哦
free(ivarList);
}
@end