Post photo on user’s wall using Facebook iOS SDK

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.

Leave a Comment