Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Facebook SDK 3 upload image with message

I am using the following code to upload an image to Facebook wall :

UIImage *img = [UIImage imageNamed:@"logoits.png"];

[FBRequestConnection startForUploadPhoto:img
                       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

    NSLog(@"Facebook posting %@",result);
    NSLog(@"Facebook posting %@",error);

}];  

It works as expected but I would like to add a message or title to this uploaded photo, and looking at the documentation of FBRequestConnection there is no example of that. http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequestConnection/

How do I upload an image with message?

like image 479
Ben Avatar asked Sep 26 '12 17:09

Ben


2 Answers

In the Facebook SDK 3.0 only a image can be post using the given methods, and for other actions you need to use graph a pi's.

So for post a image with message you need to use graph-api. Here is the line of codes which lets you to post a image with message on users timeline.

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:@"your custom message" forKey:@"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:@"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits

[FBRequestConnection startWithGraphPath:@"me/photos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) 
 {
     if (error) 
     {
         //showing an alert for failure
         [self alertWithTitle:@"Facebook" message:@"Unable to share the photo please try later."];
     } 
     else
     {
         //showing an alert for success
         [UIUtils alertWithTitle:@"Facebook" message:@"Shared the photo successfully"];
     }
     _shareToFbBtn.enabled = YES;
 }];

and also make sure _shareToFbBtn is enabled only after login is success.

like image 135
vishy Avatar answered Oct 25 '22 11:10

vishy


linesvishy's solution works great and it's also worth to note that if you were using Facebook SDK 3.1 and you used method

FBRequest *request = [FBRequest requestForUploadPhoto:_imageToShare];

You can just use the following lines to replace it:

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:_textToShare forKey:@"message"];
[params setObject:_imageToShare forKey:@"picture"];
FBRequest *request = [FBRequest requestWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST"];

Hope it helps! Happy Coding!

like image 23
sonoshin Avatar answered Oct 25 '22 11:10

sonoshin