Yes, it is better to use the AFNetworking 2.0 path with the AFHTTPRequestOperationManager . With the previous method, my file was loaded, but for some reason it was not updated on the file system.
By adding swilliam's answer to show the download process, in AFNetworking 2.0 you do the same - just install the download execution block after setting the output stream.
__weak SettingsTableViewController *weakSelf = self; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:newFilePath append:NO]; [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToRead) { float progress = totalBytesWritten / (float)totalBytesExpectedToRead; NSString *progressMessage = [NSString stringWithFormat:@"%@ \n %.2f %% \n %@ / %@", @"Downloading ...", progress * 100, [weakSelf fileSizeStringWithSize:totalBytesWritten], [weakSelf fileSizeStringWithSize:totalBytesExpectedToRead]]; [SVProgressHUD showProgress:progress status:progressMessage]; }];
This is my method of creating a byte string:
- (NSString *)fileSizeStringWithSize:(long long)size { NSString *sizeString; CGFloat f; if (size < 1024) { sizeString = [NSString stringWithFormat:@"%d %@", (int)size, @"bytes"]; } else if ((size >= 1024)&&(size < (1024*1024))) { f = size / 1024.0f; sizeString = [NSString stringWithFormat:@"%.0f %@", f, @"Kb"]; } else if (size >= (1024*1024)) { f = size / (1024.0f*1024.0f); sizeString = [NSString stringWithFormat:@"%.0f %@", f, @"Mb"]; } return sizeString; }
wzbozon Oct 21 '14 at 3:35 a.m. 2014-10-21 03:35
source share