Search code examples
afnetworkingafnetworking-3

AFNetworking migration from 1.x to 3.x


I am doing the migration of AFNetworking library from 1.x to 3.x for my project.

As per my understanding the AFHTTPRequestOperation to be replaced with AFHTTPSessionManager. What is the replacement for the method cancel and property isCancelled,isReady, request and response that are present in the AFHTTPRequestOperation class.

Help appreciated.


Solution

  • In AFHTTPRequestOperationManager

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    
        NSLog(@"JSON: %@", responseObject);
    
    } failure:^(NSURLSessionTask *operation, NSError *error) {
    
        NSLog(@"Error: %@", error);
    }];
    

    In AFHTTPRequestOperation

    NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    
        NSLog(@"JSON: %@", responseObject);
    
    } failure:^(NSURLSessionTask *operation, NSError *error) {
    
        NSLog(@"Error: %@", error);
    }];