次のコードを使用して、AFNetworking 2.0 を使用してファイルをダウンロードしようとしています。
AFHTTPRequestSerializer *serialize = [AFHTTPRequestSerializer new];
[serialize setAuthorizationHeaderFieldWithUsername...// set the auth header with username and password
serialize.timeoutInterval = 200;
NSError *error = nil;
NSMutableURLRequest *request = [serialize requestWithMethod:@"GET" URLString:url parameters:nil error:&error];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSLog(@"success….”);
NSURL *url = [NSURL new];
return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"error: %@", error.localizedDescription);
}];
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
[downloadTask resume];
}
// 進捗のためにここにオブザーバーを追加します
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
dispatch_async(dispatch_get_main_queue(), ^{
if ([keyPath isEqualToString:@"fractionCompleted"]) {
NSProgress *progress = (NSProgress *)object;
NSLog(@"Progress… %f total unit count =%lld , completed unit count =%lld", progress.fractionCompleted , progress.totalUnitCount,progress.completedUnitCount);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
});
}
observeValueForKeyPath
そして、これがメソッドからの NSLog です
SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =0
SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =975
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =25149
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =31086
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =53006
SampleDownload[5833:149754] Progress… 0.000000 total unit count =-1 , completed unit count =54377
SampleDownload[5833:149755] Progress… 0.000000 total unit count =-1 , completed unit count =66098
SampleDownload[5833:149768] Progress… 0.000000 total unit count =-1 , completed unit count =93626
SampleDownload[5833:149763] Progress… 0.000000 total unit count =-1 , completed unit count =103805
ご覧のとおり、合計ユニット数は-1です。その結果、私fractionCompleted
は 0 だと思い、UI を更新できません。私が犯している間違いは何ですか?
これを参考にしました。 AFNetworking 2.0 でダウンロードの進行状況を取得するには?
また、このスレッドで同様の議論が行われていますが、その理由を理解できませんでした。 https://github.com/AFNetworking/AFNetworking/issues/1398
編集
応答ヘッダーの投稿
{
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Headers" = "Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With";
"Access-Control-Allow-Methods" = "GET, POST, PUT, DELETE, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "public, max-age=43200";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 1166764;
"Content-Type" = "application/pdf";
Date = "Wed, 17 Dec 2014 12:17:43 GMT";
Etag = “someEtag”;
Expires = "Thu, 18 Dec 2014 00:17:43 GMT";
"Last-Modified" = "Wed, 17 Dec 2014 11:58:31 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
Vary = "Accept-Encoding";
}