0

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

Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77

1 Answers1

1

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

Your code will look like:

[[ApiClient sharedInstance] requestSerializer] = [[AFHTTPRequestSerializer serializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil];
iSashok
  • 2,326
  • 13
  • 21
  • Thanks a lot. But does it apply to the singleton ApiClient? – Lê Khánh Vinh Jul 28 '16 at 04:45
  • thanks so i'll change the request serialization to [[AFHTTPRequestSerializer serializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil]?;. how about the response serialization is this correct? – Lê Khánh Vinh Jul 28 '16 at 04:54
  • No you just need `[[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];` – iSashok Jul 28 '16 at 04:56
  • thanks, can we include these 2 in the initiation of singleton also because have arround 50 similar one everytime need these 2 setup – Lê Khánh Vinh Jul 28 '16 at 04:59
  • Yes you can if you have for all response `acceptableContentTypes ` text/html and all of your requests have a POST_METHOD type – iSashok Jul 28 '16 at 05:02
  • it will under method + (ApiClient *)sharedInstance { or - (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; }? – Lê Khánh Vinh Jul 28 '16 at 05:05