iosobjective-cafnetworking-3

AFNetworking 3.0 status code


I'm using AFNetworking 3.0 and I'm not able to get status code for success or failure.

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
                    // TODO: show progress somewhere
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSLog(@"%@",responseObject);
                    // get status here
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                    // get status here
                }];

Solution

  • Hope this helps!!!

    typedef enum RequestTypes{
    
    GET,
    POST,
    PUT,
    DELETE
    
    }RequestType;
    
    @property (nonatomic, strong) AFHTTPSessionManager *manager;    
    
    self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; // Base URL here, not entire URL
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    
     //Add Headers to request if any.  // header is a dictionary of key value pair.
    for (NSString *key in [headers allKeys]) {
        [self.manager.requestSerializer setValue:[headers valueForKey:key] forHTTPHeaderField:key];
    }
    
    /* Serialize request by type */ /* Type can be GET, POST etc */
    
    NSString *requestTypeMethod = [self getStringForRequestType:type];
    
    /* enter your URL here excluding base url  */ 
    
    NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:requestTypeMethod URLString:[[NSURL URLWithString:urlString relativeToURL:_manager.baseURL] absoluteString] parameters:params error:nil];
    
    NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    
     // here you will get actual response, NSURL response as well  and error if any.      
    
    }];
    
    [dataTask resume];
    
    
    #pragma mark - GET Request type as string
    -(NSString *)getStringForRequestType:(RequestType)type {
    
    NSString *requestTypeString;
    
    switch (type) {
        case GET:
            requestTypeString = @"GET";
            break;
    
        case POST:
            requestTypeString = @"POST";
            break;
    
        case PUT:
            requestTypeString = @"PUT";
            break;
    
        case DELETE:
            requestTypeString = @"DELETE";
            break;
    
        default:
            requestTypeString = @"GET";
            break;
     }
    
       return requestTypeString;
    }