Search code examples
iosobjective-cios6afnetworking

AFNetworking: setRedirectResponseBlock inside my class NSURLResponse always return null value


I am using AFNetworking classes to interact with web-service.I am having some problem in that.

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:theRequest];// using AFHTTPRequestOperation to request Data
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        NSLog(@"connection :%@",connection);

        NSLog(@"request :%@",request);

        NSLog(@"response :%@",redirectResponse); question 1:? i get NSURLResponse value is null. output:response :(null)

        return request;

    }];

With the help of Breakpoint i check inside the AFNetworking classes (class name : AFURLConnectionOperation.m) whether it getting response from server? Yes its getting response.

- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block {

    self.redirectResponse = block;

    NSLog(@"connection :%@",_connection);

    NSLog(@"request :%@",_request);

    NSLog(@"response :%@",_redirectResponse);//getting NSURLResponse from server.(its showing response)  output: response :<__NSGlobalBlock__: 0x46238>

    NSLog(@"block :%@",block);

}

My Problem is that when i call [operation setRedirectResponseBlock.....] inside my class NSURLResponse *redirectResponse always have null value but when i check in - (void)setRedirectResponseBlock..... inside the AFURLConnectionOperation.m class NSURLResponse *redirectResponse have some value. Can you Please Help to know that where i am wrong. last one week all days i was googling i couldn't find any answer that related to this.

i am using "theRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;" because i don't want to load all the time from server only if there any modification in header then only i want to call again. for this i need to use "setRedirectResponseBlock" to write below code.

if ([response isKindOfClass:[NSHTTPURLResponse self]]) {

NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];

NSString *modified = [headers objectForKey:@"Last-Modified"];


if (modified) { .......

Please Help me to solve this, examples much appreciated.


Solution

  • According to the documentation for the corresponding delegate method, connection:willSendRequest:redirectResponse::

    The delegate can receive this message as a result of modifying a request before it is sent, for example to transform the request’s URL to its canonical form. To detect this case, examine redirectResponse; if it is nil, the message was not sent due to a redirect.