Search code examples
objective-cafnetworking

Can anybody please explain AFHTTPSessionManager


Can anyone please explain what is happening in the following code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:[url absoluteString]
  parameters:nil
     success:^(NSURLSessionDataTask *task, id JSON) {
         NSLog(@"JSON: %@", JSON);
}

Solution

  • AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    

    This is simply an instance of the class which manages HTTPconnections.

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    

    This means that any parameters of your request (in your case there are no parameters, but usually a dictionary is passed there) will be sent over the network as JSON.

    [manager GET:[url absoluteString]
     parameters:nil
     success:^(NSURLSessionDataTask *task, id JSON) {
         NSLog(@"JSON: %@", JSON);
    }
    

    Here the manager performs GET request to the specified url without any parameters and receives some JSON data in the response block. You can turn this JSON into a dictionary or array and use according to your needs.