AFNetworking 2.0 HTTP POST Progress


How can I get AFHTTPRequest progress? I tried searching all over the net.
I use:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] }; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:@"http://localhost/thepath/isprivate/thefile.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; 

Is there any property or method that I can use to access the HTTP POST AFNetworking 2.0 progress?

+7
ios objective-c afnetworking-2
source share
3 answers

You can use the following method

 - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData progress:(NSProgress * __autoreleasing *)progress completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler 

class AFHTTPSessionManager .

UPDATE:

Usually you prefer to use KVO to load values. You need to use something like the following:

 static void * kDGProgressChanged = &kDGProgressChanged; ... [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:kDGProgressChanged]; ... - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(__unused NSDictionary *)change context:(void *)context { if (kDGProgressChanged == context) { dispatch_async(dispatch_get_main_queue(), ^{ [self updateProgressInfo]; }); } } 
+1
source

Try the following:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] }; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://localhost/thepath/isprivate/thefile.php" parameters:params error:&error]; AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"operation success: %@\n %@", operation, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite; NSLog(@"progress updated(percentDone) : %f", percentDone); }]; [requestOperation start]; 

Hope this helps.

+19
source

The shortest way to implement this

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *requestOperation = [manager POST:@"/service/user/authenticate" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //Your Business Operation. NSLog(@"operation success: %@\n %@", operation, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; //Here is the upload progress [requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite; //Upload Progress bar here NSLog(@"progress updated(percentDone) : %f", percentDone); }]; 
+10
source

All Articles