Wednesday, September 12, 2012

Show progress while uploading data to Facebook

Facebook's SDK does not provide the delegate function to show the progress during uploading the data to Facebook, e.g. photo. Therefore, user has to create it by adding the following functions into FBRequest.h and FBRequest.m as well as your ViewController.

In FBRequest.h, add the below function under FBRequestDelegate protocol:

- (void)request:(FBRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;



In FBRequest.m, add the below function:

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
   
if ([_delegate respondsToSelector:@selector(request:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)]) {
       
[_delegate request:self didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
   
} }




In your ViewController, add the below function to implement the delegate function:

- (void)request:(FBRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
   
NSLog(@"%d bytes out of %d sent.", totalBytesWritten, totalBytesExpectedToWrite); } 


Right now, your ViewController can receive the feedback from FBRequest when the data is being uploaded to Facebook.