Search code examples
iphoneiosfacebookfacebook-graph-apifacebook-ios-sdk

How to post to a users wall using Facebook SDK


I want to post some text to a users wall using the facebook sdk in an iOS app.

Is posting an open graph story now the only way to do that?

I've found with open graph stories they are really strange, you can only post things in the format "user x a y" where you preset x and y directly on facebook, like user ata a pizza or user played a game. Setting up each one is pretty laborious too because you have to create a .php object on an external server for each one.

Am I missing something or is there a simpler way to go about this?


Solution

  • Figured it out by browsing the facebook tutorials a bit more.

    -(void) postWithText: (NSString*) message
               ImageName: (NSString*) image
                     URL: (NSString*) url
                 Caption: (NSString*) caption
                    Name: (NSString*) name
          andDescription: (NSString*) description
    {
    
        NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       url, @"link",
                                       name, @"name",
                                       caption, @"caption",
                                       description, @"description",
                                       message, @"message",
                                       UIImagePNGRepresentation([UIImage imageNamed: image]), @"picture",
                                       nil];
    
        if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
        {
            // No permissions found in session, ask for it
            [FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:@"publish_actions"]
                                                  defaultAudience: FBSessionDefaultAudienceFriends
                                                completionHandler: ^(FBSession *session, NSError *error)
            {
                 if (!error)
                 {
                     // If permissions granted and not already posting then publish the story
                     if (!m_postingInProgress)
                     {
                         [self postToWall: params];
                     }
                 }
             }];
        }
        else
        {
            // If permissions present and not already posting then publish the story
            if (!m_postingInProgress)
            {
                [self postToWall: params];
            }
        }
    }
    
    -(void) postToWall: (NSMutableDictionary*) params
    {
        m_postingInProgress = YES; //for not allowing multiple hits
    
        [FBRequestConnection startWithGraphPath:@"me/feed"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error)
         {
             if (error)
             {
                 //showing an alert for failure
                 UIAlertView *alertView = [[UIAlertView alloc]
                                           initWithTitle:@"Post Failed"
                                           message:error.localizedDescription
                                           delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
                 [alertView show];
             }
             m_postingInProgress = NO;
         }];
    }