我正在开发一款涉及从网络服务器下载数据的iPhone应用程序。除了方法connection:willCacheResponse:
永远不会被调用之外,一切正常。但其他人如connection:didReceiveResponse:
,connection:didReceiveData:
,connection:didFailWithError:
,connectionDidFinishLoading:
一切正常。我建立了如下连接:
- (void)makeConnection
{
NSURL *url = [NSURL URLWithString:@"http://www.abc.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"%@", string];
[request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSCachedURLResponse *newCachedResponse = cachedResponse;
NSDictionary *newCachedResponseUserInfo = [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"Cached Date"];
newCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:[cachedResponse data] userInfo:newCachedResponseUserInfo storagePolicy:[cachedResponse storagePolicy]];
return newCachedResponse;
}
我也尝试将政策更改为NSURLRequestUseProtocolCachePolicy
,但没有任何帮助。还尝试在
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
但没有发生任何事。 我错过了什么吗?提前谢谢。
答案 0 :(得分:6)
connection:willCacheResponse:仅在响应包含Cache-Control标头时被调用,根据Apple’s documentation:
委托接收连接:willCacheResponse:仅针对支持缓存的协议的消息。