画像とキャッシュも保存する優れたライブラリとしてSDWebImageフレームワークを使用しています。
現在、他のタイプのファイルもダウンロードしようとしていますが、SDWebImage は現在役に立ちません。
AFHTTPRequestOperation
ファイルのダウンロードに使用していますが、正しくダウンロードされています。
NSURL *url = [NSURL URLWithString:@"http://mydomain/file.zip"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[url lastPathComponent]];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"bytesRead: %u, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"RES: %@", [[[operation response] allHeaderFields] description]);
NSError *error;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:&error];
if (error) {
NSLog(@"ERR: %@", [error description]);
} else {
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(@"OK");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERR: %@", [error description]);
}];
[operation start];
しかし、キャッシュを使用したいのですが、キャッシュを使用せずに常にダウンロードすると、サーバーは次のようなヘッダーに応答します。
RES: {
"Accept-Ranges" = bytes;
"Content-Length" = 2866793;
"Content-Type" = "application/vnd.apple.pkpass";
Date = "Fri, 22 Aug 2014 09:10:11 GMT";
Etag = "\"d9c347d85aabcf1:0\"";
"Last-Modified" = "Tue, 29 Jul 2014 18:28:08 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "ASP.NET";
}
AFHTTPRequest
キャッシュの使用を強制するために不足しているものはありますか? またはSDWebImage
、他のタイプのファイルのような利用可能なライブラリはありますか?