连接:willCacheResponse将永远不会被调用
问题描述:
我正在研究一个涉及从Web服务器下载数据的iPhone应用程序。除了方法connection:willCacheResponse:
永远不会被调用,所有的东西都正常工作。但其他人喜欢connection:didReceiveResponse:
,connection:didReceiveData:
,connection:didFailWithError:
,connectionDidFinishLoading:
所有工作正常。我喜欢做以下连接:连接:willCacheResponse将永远不会被调用
- (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
但什么也没有发生。 我错过了什么吗?先谢谢你。
答
连接:willCacheResponse:如果所述响应包含Cache-Control头,仅称为根据Apple’s documentation:
的委托接收连接:willCacheResponse:消息仅对支持高速缓存协议。