AFNetworking: setRedirectResponseBlock inside my class NSURLResponse always return null value

夙愿已清 提交于 2020-01-25 21:08:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17854142/afnetworking-setredirectresponseblock-inside-my-class-nsurlresponse-always-retu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!