Search code examples
iosfacebookfacebook-ios-sdk

iPhone - How to post image on friend's facebook wall


if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
{
        // No permissions found in session, ask for it
    [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                                   defaultAudience:FBSessionDefaultAudienceFriends
                                                 completionHandler:^(FBSession *session, NSError *error)
    {
             // If permissions granted, publish the story
         if (!error)
         {
             [self postImageToFB] ;
         }
    }];
}
    // If permissions present, publish the story
else
{
    [self postImageToFB] ;
}
- (void) postImageToFB
{
    NSData* imageData = UIImageJPEGRepresentation(self.image, 90);
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"test", @"message",
                                    imageData, @"source",
                                    nil];

    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/photos",friendName]
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
    {
         NSLog(@"%@",error) ;
         if( error == NULL )
         {
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                             message:@"Post sucessed!!"
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil] ;
             [alert show] ;
         }
    }];
}

I can use - (void) postImageToFB function to post on my own wall when I change [NSString stringWithFormat:@"%@/photos",friendName] to @"me/photos"

But I can't post on my friend's wall , maybe permission is wrong or there are some problem I didn't know ?

I got these error

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xc0a7320 {com.facebook.sdk:ErrorInnerErrorKey=Error 
Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xc001ea0 {NSUnderlyingError=0xaa45270 "bad URL", NSLocalizedDescription=bad URL}, com.facebook.sdk:HTTPStatusCode=200}

Solution

  • You cant Post to friends wall from now...

    Removing ability to post to friends walls via Graph API We will remove the ability to post to a user's friends' walls via the Graph API. Specifically, posts against [user_id]/feed where [user_id] is different from the session user, or stream.publish calls where the target_id user is different from the session user, will fail. If you want to allow people to post to their friends' timelines, invoke the feed dialog. Stories that include friends via user mentions tagging or action tagging will show up on the friend’s timeline (assuming the friend approves the tag). For more info, see this blog post.

    Check this...changes mentioned at facebook developers portal https://developers.facebook.com/roadmap/completed-changes/#february-2013