Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post photo on user's wall using Facebook iOS SDK

I'm trying to upload a photo from the camera to a user's Facebook wall. I'm not entirely sure what the correct strategy is, but from reading around it seems the thing to do is upload the photo to an album, and then someone post on the wall a link to that album/photo. Ideally this would involve the dialog, but from what I can tell that's not possible.

I've managed to upload a photo to an album, and get back an ID for that photo, but I'm not sure what to do after that.

Can anyone provide some straightforward code for achieving this?

Bonus question: Is it possible to post the photo to the application wall, as well (or instead)?

Edit: Graph API is preferable, but anything that works at this stage is good.

like image 358
Ben Williams Avatar asked Jul 15 '11 02:07

Ben Williams


People also ask

Can I post on Facebook using API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.


2 Answers

I did this in three steps

1 post picture to album (returns imageID)

2 use imageID to request metadata for imageID

3 use the 'link' field (not the 'source' field) as a link to the image in a post to the user's wall

The downside is that there are now two posts to the wall, one for the image, and one for the actual post. I haven't figured out yet how to post a picture to an album, without also a wall post appearing (ideally it would just be the 2nd post that appears)

Step 1:

- (void) postImageToFacebook {      appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];      currentAPICall = kAPIGraphUserPhotosPost;      UIImage *imgSource = {insert your image here};     NSString *strMessage = @"This is the photo caption";     NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:                                          imgSource,@"source",                                          strMessage,@"message",                                          nil];      [appDelegate.facebook requestWithGraphPath:@"me/photos"                                      andParams:photosParams                                  andHttpMethod:@"POST"                                    andDelegate:self];           // after image is posted, get URL for image and then start feed dialog     // this is done from FBRequestDelegate method } 

Step 2 (kAPIGraphUserPhotosPost) & step 3 (kAPIGraphPhotoData):

- (void)request:(FBRequest *)request didLoad:(id)result {      if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {         result = [result objectAtIndex:0];     }      switch (currentAPICall) {         case kAPIGraphPhotoData: // step 3         {             // Facebook doesn't allow linking to images on fbcdn.net.  So for now use default thumb stored on Picasa             NSString *thumbURL = kDefaultThumbURL;             NSString *imageLink = [NSString stringWithFormat:[result objectForKey:@"link"]];                  currentAPICall = kDialogFeedUser;             appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];               NSMutableDictionary* dialogParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:                                      kAppId, @"app_id",                                      imageLink, @"link",                                      thumbURL, @"picture",                                      @"Just Played Wizard etc etc", @"name",                                      nil];              [appDelegate.facebook dialog:@"feed"                                 andParams:dialogParams                               andDelegate:self];               break;         }         case kAPIGraphUserPhotosPost: // step 2         {              NSString *imageID = [NSString stringWithFormat:[result objectForKey:@"id"]];                         NSLog(@"id of uploaded screen image %@",imageID);              currentAPICall = kAPIGraphPhotoData;             appDelegate = (Scorecard4AppDelegate *)[[UIApplication sharedApplication] delegate];              [appDelegate.facebook requestWithGraphPath:imageID                                            andDelegate:self];             break;         }     } } 

I've modified the code to show just the Facebook stuff condensed. If you want to check if the post is successful you'll want something like this:

- (void)dialogDidComplete:(FBDialog *)dialog {     switch (currentAPICall) {         case kDialogFeedUser:         {             NSLog(@"Feed published successfully.");             break;         }     } } 

facebook post

The blue text in the Facebook post is whatever you put in the "name" parameter in Step 3. Clicking on the blue text in Facebook will take you to the photo posted in Step 1, in an album in Facebook (Facebook creates a default album for your app if you don't specify an album). In my app's case it's the full-sized image of the scorecard (but could be any image, e.g. from the camera). Unfortunately I couldn't figure out a way to make the thumb image a live link, but it's not very readable so a default thumb works in my case. The part in the Facebook post (in black font) that says "First game of the year..." is entered by the user.

like image 84
Eric D'Souza Avatar answered Oct 25 '22 21:10

Eric D'Souza


It's clearer what you wish to do - post a photo to FB, and guarantee that a post goes on the user's wall/stream.

Unfortunately, there are some things in the way.

FB Graph API appears to only allow you to post EITHER a picture to an album, or post to the wall directly, linking to a picture already existing somewhere on the web. In the first case, a post in the stream will probably be made, but FB appears to consolidate multiple posts in some manner so as to keep the user's stream from being bombarded. The mechanism for this is not documented anywhere I could see.

In the second case, you might think you could get away with posting to an album, and then explicitly posting a link to the album. You can add a parameter to the original album post, "no_story" with a value of 1, and suppress the wall post that might be made while you prepare to make an explicit one. However, FB will not have the source URL for a newly posted image for a while, AND, it doesn't appear to like URLs that include its own content delivery network, returning an error. You might think to simply put status update in the stream, talking about the post, However, the Graph API is also limited to 25 such direct feed posts per day per app, to prevent spamming.

One solution would be to post to something like Flickr, get the URL of the image, and then post to the wall. FB's preferred solution appears to be to use the FB dialogs that are part of the mobile toolkit - essentially little web pages much like the OAuth screen.

Personally, I plan to simply post to the album as above, and live with FB's idea of how the user should be notified. Curious how you choose to proceed.

like image 39
giff Avatar answered Oct 25 '22 23:10

giff