Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading photo with caption or status to Facebook on iPhone app

I managed to implement Facebook connect on my iPhone app and allow the user to upload a status update via the app. I now want to include a feature to allow uploading of photos together with a caption or status update. Does anyone know the relevant code for this?

I think I'm supposed to use the photos.upload API in FB but am not too sure how. The Facebook documentation all seems very generic with no specific code for iPhone development. Any assistance or sample code with be appreciated.

like image 317
leeks Avatar asked Aug 02 '10 18:08

leeks


1 Answers

This following snippet is taken from the facebook-ios-sdk. Assuming your class already has a facebook object, this method will upload a photo from a URL (change as needed):

-(IBAction) uploadPhoto: (id)sender {
  NSString *path = @"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg";
  NSURL *url = [NSURL URLWithString:path];
  NSData *data = [NSData dataWithContentsOfURL:url];
  UIImage *img = [[UIImage alloc] initWithData:data];

  NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                  img, @"picture",
                                  nil];
  [facebook requestWithMethodName: @"photos.upload"
                         andParams: params
                     andHttpMethod: @"POST"
                       andDelegate: self];
  [img release];
}

In order to send a caption with the photo you must add the "message" parameter key followed by the caption text. Changing params like below should achieve this:

NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                img, @"picture", 
                @"my photo's caption text here.", @"message"
                                nil];
like image 193
dredful Avatar answered Sep 30 '22 11:09

dredful