AFNetworking 动态修改acceptableContentTypes 设置ContentType


AFJSONResponseSerializer+Serializer.h

#import <AFNetworking/AFNetworking.h>

@interface AFJSONResponseSerializer (Serializer)

@end

AFJSONResponseSerializer+Serializer.m

#import "AFJSONResponseSerializer+Serializer.h"
#import <objc/runtime.h>

@implementation AFJSONResponseSerializer (Serializer)


+(void)load {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method ovr_initMethod = class_getInstanceMethod([self class], @selector(init));
        Method swz_initMethod = class_getInstanceMethod([self class], @selector(swizzlingForSetSerializer_init));
        method_exchangeImplementations(ovr_initMethod, swz_initMethod);
    });
    
}


- (id) swizzlingForSetSerializer_init {
    
    id swz_self = [self swizzlingForSetSerializer_init];
    if (swz_self && [swz_self isKindOfClass:[AFJSONResponseSerializer class]]) {
        //start  tiny
        NSSet * contentSet = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/plain", @"text/html", nil];
        [swz_self setValue:contentSet forKey:@"acceptableContentTypes"];
    }
    else {
        NSLog(@"AFJSONResponseSerializer+Serializer kvc get AFJSONResponseSerializer error");
    }
//
    return swz_self;
}


@end


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM