I know this issue has been very popular on this Site, but after searching most of answers, I couldn't find out the solution.
My app is to upload an image to the remote url. I tried several ways to upload and all of them successful:
(1) Use AFURLSessionManager
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[(AFHTTPResponseSerializer*)manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"multipart/form-data", nil]];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSDictionary *userInfo=[error userInfo];
id responseError=[userInfo objectForKey:@"AFNetworkingOperationFailingURLResponseErrorKey"];
if ([responseError isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *reponse=(NSHTTPURLResponse*)responseError;
if (reponse.statusCode==200) {
result(YES);
} else {
result(NO);
}
} else {
result(NO);
}
} else {
result(YES);
}
}];
[uploadTask resume];
For this way, I used KVO to monitor progress, but no luck.
(2) Use AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"total bytes written: %lld",totalBytesWritten);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
result(YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.response.statusCode==200) {
result(YES);
} else {
result(NO);
}
}];
[operation start];
It only shows the log "total bytes written:" only once with 100% volume of the file immediately when starting uploading.
(3) Use AFHTTPRequestOperationManager
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
result(YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.response.statusCode==200) {
result(YES);
} else {
result(NO);
}
}];
// 4. Set the progress block of the operation.
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
NSLog(@"progress updated(percentDone) : %f", percentDone);
}];
// 5. Begin!
[operation start];
=> Same result to the 2nd solution.
Notice that all solutions do uploading successfully but the progress not updated (NSProgress
or block).