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
原文地址:https://www.cnblogs.com/qqcc1388/p/6543040.html