7


How can I get the progress of an AFHTTPRequest? I've tried searching all over the net.
I am using:

    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 like, a property or method I can use to access the progress of an AFNetworking 2.0 HTTP POST?

Matthias Bauch
  • 88,097
  • 19
  • 217
  • 244
DHShah01
  • 537
  • 1
  • 7
  • 16

3 Answers3

19

Try this:

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.

Yas Tabasam
  • 10,221
  • 9
  • 45
  • 53
10

The shortest way way to implement this is

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);
        }];
Igbalajobi Jamiu
  • 261
  • 3
  • 13
  • It looks like you are setting the progress block inside the success block. Does that really work? I would think that would be useless you're basically setting the progress block after it has successfully completed. – Sterling Christensen Dec 27 '14 at 04:53
  • @SterlingChristensen Thanks for you notification. The code has been edited appropriately. Thank – Igbalajobi Jamiu Dec 28 '14 at 08:25
1

You can use following method

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

of AFHTTPSessionManager class.

UPDATE:

Usually you will prefer to use KVO to get uploading values. So something like following should be used:

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];
        });
    }
}
Avt
  • 16,037
  • 4
  • 48
  • 67
  • I used this method but the progress not called. The upload action is done successfully, file was uploaded but progress not tracked although I used KVO to monitor. What happens? – lenhhoxung Jun 17 '14 at 11:06