Search code examples
iosfacebooksharekit

iOS & Facebook: upload an image along with user comment to the wall


I have an app, it takes screenshot of itself and I would like to post it on user's wall along with a comment.

The problem is, I seemingly tried every option and none of it works:

  • [facebook dialog:@"feed" ...] doesn't work because you can supply only a URL, not a UIImage.

  • [facebook requestWithGraphPath ...] doesn't work because the user is not prompted for the comment.

  • I could first (silently) try to upload the screenshot with requestWithGraphPath and then feed its newly created URL to dialog which doesn't work because of "FBCDN image is not allowed in stream". All of this is of course using callbacks which, including login callback, makes for nice big mess.

  • I even tried ShareKit but forgot about it after the app strangely could/couldn't login.

Please, how can I share both an image and a comment?


Solution

  • If you are using facebook's latest iOS SDK, in Hackbook sample app, look for APICallsViewController.m file. There is this method that you can post using UIImage directly:

    /*
     * Graph API: Upload a photo. By default, when using me/photos the photo is uploaded
     * to the application album which is automatically created if it does not exist.
     */
    - (void) apiGraphUserPhotosPost {
        NSString *sourcePath = [[NSBundle mainBundle] resourcePath];
        NSString *file1 = [sourcePath stringByAppendingPathComponent:@"/MP1101frame1002.jpg"];
        UIImage *img = [[[UIImage alloc]initWithContentsOfFile:file1]autorelease];
    
        [self showActivityIndicator];
        currentAPICall = kAPIGraphUserPhotosPost;
        HackbookAppDelegate *delegate = (HackbookAppDelegate *) [[UIApplication sharedApplication] delegate]; 
    
        // Download a sample photo
        NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
    //    UIImage *img  = [[UIImage alloc] initWithData:data];   //here you can insert your UIImage
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       yourMessageHere, @"message",  //whatever message goes here
                                       img, @"picture",   //img is your UIImage
                                       nil];
        [[delegate facebook] requestWithGraphPath:@"me/photos"
                              andParams:params
                          andHttpMethod:@"POST"
                            andDelegate:self];
    }