1

I am working on a project that using AFNetworking to connect with API interface. My problem is that how to send a request to the backend with body content that includes email, deviceId. I have found many solutions that all compatible with AFNetworking 2.0 not 3.0.

Now I am using SessionManager, when i initialize request, how can I add content body context?

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
NSString *stringData = [[NSString alloc]initWithData:jsonData encoding:NSASCIIStringEncoding];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
[manager POST:_urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSString *link = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"JSON: %@",link);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

I have tried to put email& deviceId into [manager POST parameter: Dict], but it is not working.

Can someone tell me how to add body to AFNetworking 3.0? Thanks

trungduc
  • 11,926
  • 4
  • 31
  • 55
Janice Zhan
  • 571
  • 1
  • 5
  • 18
  • You'd better create your own singleton rather than use manager of AFNetworking. I found that its manager will override your header when send request, and the manager will cause memory leak. You can implement your manager. – Kim Jin Jul 25 '17 at 03:15
  • @S.Jin Can you give the example? I am new to ios& objective c and I cannot understand that – Janice Zhan Jul 25 '17 at 03:23
  • @JaniceZhan You can inherit `AFHTTPSessionManager` and create a singleton. Like this:`+ (instancetype)sharedManager { static MyManager *manager = nil; static dispatch_once_t pred; dispatch_once(&pred, ^{ manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://baseURL.org/"]]; }); return manager; } `. You also can set your header field while send request or in the init method. – Kim Jin Jul 25 '17 at 07:03

1 Answers1

0

if i got your question correctly it shouldnt be that hard. its similar to the 2.0 version as well.

NSDictionary *parameters = @{@"username": _username.text,
                             @"deviceId": deviceIdString,

                             };



NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];

    NSURL *baseURL = [NSURL URLWithString:@"http://url.com/ws/test.php"];

    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:@"" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        //Response

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if(error.code==-1001){
            //Handle the error

        }

    }];
Im Batman
  • 1,842
  • 1
  • 31
  • 48