2012-08-16 36 views
3

我正在研究一個涉及從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 

但什麼也沒有發生。 我錯過了什麼嗎?先謝謝你。

回答

6

連接:willCacheResponse:如果所述響應包含Cache-Control頭,僅稱爲根據Apple’s documentation:

的委託接收連接:willCacheResponse:消息僅對支持高速緩存協議。

相關問題