10

I needed to publish a picture on my wall. The pic is generated in my iPad app.

Fede Cugliandolo
  • 1,686
  • 15
  • 24

2 Answers2

24

This is the simplest way I've found

- (void) postImageToFB:(UIImage*)image
{
    NSData* imageData = UIImageJPEGRepresentation(image, 90);    
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"This is my drawing!", @"message",
                                    imageData, @"source",
                                    nil];

    [FBRequestConnection startWithGraphPath:@"me/photos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                          }];
}

if you want to post on a friend's wall, change @"me/photos" by @"[friendID]/photos"

Then, ask for permissions to publish and call the method

if ([FBSession.activeSession.permissions indexOfObject:@"publish_stream"] == NSNotFound)
{
    // No permissions found in session, ask for it
    [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_stream"]
                                               defaultAudience:FBSessionDefaultAudienceFriends
                                             completionHandler:^(FBSession *session, NSError *error)
     {
         // If permissions granted, publish the story
         if (!error) [self postImageToFB:currentDrawing];
     }];
}
// If permissions present, publish the story
else [self postImageToFB:currentDrawing];

An "[App Name] Photos" album will be created, if doesn't exist

It does work for me!

Fede Cugliandolo
  • 1,686
  • 15
  • 24
  • 4
    no need of NSData, just save it as an UIImage and change the image key as "picture", graph API will handle the rest :-) – DD_ Jan 11 '13 at 04:41
  • Your compression on at UIImageJPEGRepresentation should be between 0-1.0.. so instead of 90 you mean .9 – badweasel Oct 17 '13 at 08:14
-1

For iOS from 4.3 and the UI look like iOS 6.0, I think you want something like this: IOS sharing framework for twitter, facebook, flicr, tumblr

Community
  • 1
  • 1
huyleit
  • 175
  • 1
  • 8