メソッド「sendAsynchronousRequest」を使用するだけです。ここに私の解決策があり、それは魅力のように機能します。
[NSURLConnection sendAsynchronousRequest:newRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data,NSError *error){
NSLog(@"--- caching ---");
if (error) {
NSLog(@"%@", error);
NSLog(@"not cached: %@", absoluteString);
}
NSString *filename = [self sha1:absoluteString];
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager createFileAtPath:path contents:data attributes:nil];
[fileManager release];
NSURLResponse *newResponse = [[NSURLResponse alloc] initWithURL:response.URL MIMEType:response.MIMEType expectedContentLength:data.length textEncodingName:nil];
NSDictionary *responseInfo = [NSDictionary dictionaryWithObjectsAndKeys:filename, @"filename", newResponse.MIMEType, @"MIMEType", nil];
[responsesInfo setObject:responseInfo forKey:absoluteString];
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:newResponse data:data];
[newResponse release];
[cachedResponses setObject:cachedResponse forKey:absoluteString];
[cachedResponse release];
}];
iOS5.0以降でしか利用できないためです。最初に iOS のバージョンを確認する必要があるかもしれません。