您可以将 AFURLConnectionOperation 子类化以具有 2 个新属性:(NSInteger)totalBytesSent
和(NSInteger)totalBytesExpectedToSend
。您应该像这样在 NSURLConnection 回调中设置这些属性:
- (void)connection:(NSURLConnection *)__unused connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
[super connection: connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
self.totalBytesSent = totalBytesWritten;
self.totalBytesExpectedToSend = totalBytesExpectedToSend;
}
您的 uploadProgress 块可能如下所示:
……(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSInteger queueTotalExpected = 0;
NSInteger queueTotalSent = 0;
for (AFURLConnectionOperation *operation in self.operationQueue) {
queueTotalExpected += operation.totalBytesExpectedToSend;
queueTotalSent += operation.totalBytesSent;
}
self.totalProgress = (double)queueTotalSent/(double)queueTotalExpected;
}];