未访问cachedResponseForRequest方法

时间:2012-03-30 00:38:12

标签: iphone ios nsurlconnection nsurlcache

我正在尝试设置一个缓存,但我正在使用'如下所示'的方法没有被线程访问。

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

我正在初始化这样的连接,并且访问了connectionDidFinishLoading,所以我不确定我错过了什么。

- (IBAction)searchRequest:(NSData *)postBodyData
{
    //Set database address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"https://127.0.0.1:88"]; 

    NSURL *url = [NSURL URLWithString:databaseURL];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postBodyData length]];

    //SynchronousRequest to grab the data, also setting up the cachePolicy
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; //if request dose not finish happen within 60 second timeout.

//    NSInputStream *fileStream = [NSInputStream inputStreamWithData:postBodyData];


    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/octet-stream" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody:postBodyData];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [NSMutableData data];
    } else {
        // Inform the user that the connection failed from the connection:didFailWithError method
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

只有在缓存响应的情况下才会调用

connection:willCacheResponse:。在大多数情况下,POST请求不可缓存。 (更多细节:Is it possible to cache POST methods in HTTP?

你应该看看像MKNetworkKit那样处理很多这种缓存的东西,特别是对于REST协议。

您还可以查看Drop-in offline caching for UIWebView。您必须对其进行重大修改,但可以使用NSURLProtocol来解决此类问题。 AFCache目前正致力于整合这种方法,是另一个需要考虑的工具包。 (阅读博客文章中的评论,了解有关问题的更多背景信息。)

相关问题