Suraj Sukale Suraj Sukale - 1 year ago 265
iOS Question

How to POST data to server and Get Response from server using AFNetworking 3.0?

I'm very new in iPhone application Development.Please Help me in How to POST data to server and Get Response from server using AFNetworking 3.0?
My requirement is when user click on submit button then the number entered in text field send to server and from server i want to get Token_id or password(whatever,take any one).I tried like following way;

NSString *Loginurl = [NSString stringWithFormat:@"https://my_url"];

NSDictionary *params = @{@"mobile":self.phonenumber.text,



AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil];

[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {

NSLog(@"Response from server 1 : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

[self getdata:responseObject];

failure:^(NSURLSessionTask *operation, NSError *error)


NSLog(@"Error: %@", error);

please give me suggestion regarding this problem.

Answer Source

See this example may be it will help you:-

NSString *bURL = @"";
NSString *path = @"method/url/";

NSMutableDictionary *param = [NSMutableDictionary dictionary];
[parameters setObject:@"yourDictValue" forKey:@"YourDictkey"];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:bURL];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:path parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"JSON: %@", responseObject);
        //here is place for code executed in success case

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

  UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                            message:@"Sorry, try again."
        [alertV show];

        NSLog(@"Err: %@", [error localizedDescription]);

for more Information see this question