Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2.0 HTTP POST Progress


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?

like image 742
DHShah01 Avatar asked Feb 22 '14 08:02

DHShah01


3 Answers

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.

like image 80
Kameshwar Sheoran Avatar answered Nov 16 '22 03:11

Kameshwar Sheoran


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);
        }];
like image 10
Igbalajobi Jamiu Avatar answered Nov 16 '22 01:11

Igbalajobi Jamiu


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];
        });
    }
}
like image 1
Avt Avatar answered Nov 16 '22 02:11

Avt