对于下载文件,我这样做:
GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:url];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error)
{
[data writeToFile:localFilePath atomically:YES];
}];
文件已成功下载,但如果您这样做,我无法获得下载进度。
fetcher.downloadProgressBlock = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
{
NSLog(@"bytesWritten = %lld",bytesWritten);
NSLog(@"totalBytesWritten = %lld",totalBytesWritten);
NSLog(@"totalBytesExpectedToWrite = %lld",totalBytesExpectedToWrite);
};
我究竟做错了什么?
我找到了可行的解决方案。
float totalBytesExpectedToWrite = [file.size floatValue]; //file - it's GTLDriveFile to download
[fetcher setReceivedProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten)
{
NSLog(@"Download progress - %.0f",(totalBytesWritten * 100)/totalBytesExpectedToWrite);
}];