tommi
tommi

Reputation: 6973

How can I check the progress of my Facebook iOS upload?

I'm using the Facebook iOS SDK and using the Graph API to upload videos to Facebook.

The uploading is working perfectly fine, but can I keep track of the progress of the upload so I can reflect the progress in a progress bar.

Upvotes: 2

Views: 12612

Answers (2)

Nur Iman Izam
Nur Iman Izam

Reputation: 1613

This is an old question but what you're trying to do is possible with latest Facebook iOS SDK v3.9. (27 Oct 2013)

Essentially, FBRequestConnection exposes a property urlRequest (NSMutableURLRequest) that you can use to send out the data any other third party networking frameworks or even the ones Apple provided.

https://developers.facebook.com/docs/reference/ios/current/class/FBRequestConnection#urlRequest

Here's an example how I get progress callbacks using AFNetworking 1.x.

Prepare Request Body

NSDictionary *parameters = @{ @"video.mov": videoData,
                              @"title": @"Upload Title",
                              @"description": @"Upload Description" };

Create FBRequest

FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos" 
                                          parameters:parameters
                                          HTTPMethod:@"POST"];

Generate FBRequestConnection (Cancel & Extract URLRequest)

FBRequestConnection *requestConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[requestConnection cancel];

NSMutableURLRequest *urlRequest = requestConnection.urlRequest;

Use AFNetworking HTTPRequestOperation

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  // Do your success callback.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  // Do your failure callback.
}];

Set Progress Callback

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

Start the operation

[[APIClient sharedInstance] enqueueHTTPRequestOperation:operation];
// APIClient is a singleton class for AFHTTPClient subclass

Upvotes: 5

Baza207
Baza207

Reputation: 2142

I've finally found a way of doing this after looking around in NSURLConnection. It means adding the following code inside of the FBRequest.h and FBRequest.m files to create a new delegate.

At the bottom of the FBRequest.m file there are all of the methods for NSURLConnectionDelegate. Add this code here:

- (void)connection:connection
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    float percentComplete = ((float)totalBytesWritten/(float)totalBytesExpectedToWrite);

    if ([_delegate respondsToSelector:@selector(request:uploadPercentComplete:)])
    {
        [_delegate request:self uploadPercentComplete:percentComplete];
    }
}

Now put this in the FBRequest.h class to create a new FBRequest delegate:

/**
 * Called a data packet is sent
 *
 * The result object is a float of the percent of data sent
 */
- (void)request:(FBRequest *)request uploadPercentComplete:(float)per;

This goes at the bottom of the FBRequest.h file after:

@protocol FBRequestDelegate <NSObject>

@optional

Now all you have to do is call this new delegate anywhere in your code like you would any other FBRequest delegate and it will give you a float from 0.0 to 1.0 (0% to 100%).

Strange that the Facebook API doesn't have this (along with upload cancel which I found out how to do here How to cancel a video upload in progress using the Facebook iOS SDK?) as it's not that tricky.

Enjoy!

Upvotes: 0

Related Questions