Lê Khánh Vinh Lê Khánh Vinh - 2 months ago 50x
Objective-C Question

setup AFNetworking 3.x singleton Request and Response serialization

Hi i'm using AFNetworking 3.x. I have built a singleton for network api

+ (ApiClient *)sharedInstance {
static ApiClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];

return sharedInstance;

- (id)initWithBaseURL:(NSURL *)url
if ((self = [super initWithBaseURL:url])) {
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

return self;

i want to setup the Request and Response Serialisation when using the singleton. How can we do that

I write these code keep getting error. any help is much appreciate. Thanks

[[[ApiClient sharedInstance]requestSerializer = [AFHTTPRequestSerializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil]];

my response serialization

ApiClient sharedInstance].responseSerializer.acceptableContentTypes = [[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

enter image description here


You need change your call [AFHTTPRequestSerializer requestWithMethod..] to

[[AFHTTPRequestSerializer serializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil];

AFHTTPRequestSerializer don't have a class method requestWithMethod it need the first create a instance and after call requestWithMethod