我会为此使用AFNetworking。AFHTTPRequestOperation允许您设置用于跟踪进度的块。
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:YOUR_URL_REQUEST];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// Completion code.
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// Error handling code.
}];
以下是跟踪进度的方法:
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
float progress = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite));
// Track the progress here
}];