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.
Join Stack Overflow to learn, share knowledge, and build your career.
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.
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.
NSDictionary *parameters = @{ @"video.mov": videoData,
@"title": @"Upload Title",
@"description": @"Upload Description" };
FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos"
parameters:parameters
HTTPMethod:@"POST"];
FBRequestConnection *requestConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[requestConnection cancel];
NSMutableURLRequest *urlRequest = requestConnection.urlRequest;
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.
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[[APIClient sharedInstance] enqueueHTTPRequestOperation:operation];
// APIClient is a singleton class for AFHTTPClient subclass
NSData
instead of a NSDictionary
or something. How would I fix this?
– yoeriboven
May 10 '14 at 12:49
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!