0

As an incident of a user taking some action in my app, I want to post an image to Facebook on their behalf. Let's assume the user has already granted me publish_actions permission in class LoginVC (one time permission is used for ad infinitum posting in the future). Then at some in the future, in ActionVC, I want to publish a photo to Facebook. How do I do that? Here is the method I need to implement:

- (void)publishPhoto:(UIImage *)image
{
  //what goes in here?
}

So far I have been looking at the samples from Facebook. The closest I come is the following, but it seems to be using a Dialog. But I don't want the user to "know" that the photo is being posted. They already granted the permission and I want the posting to happen without their knowledge as it were. So some other action has triggered the call to publish...

For reference, the code from the Facebook sample looks like this

- (void)publishPhoto:(UIImage *)image
{
    BOOL canPresent = [FBDialogs canPresentShareDialogWithPhotos];
    NSLog(@"canPresent: %d", canPresent);

    FBPhotoParams *params = [[FBPhotoParams alloc] init];
    params.photos = @[image];

    BOOL isSuccessful = NO;
    if (canPresent) {
        FBAppCall *appCall = [FBDialogs presentShareDialogWithPhotoParams:params
                                                              clientState:nil
                                                                  handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                                      if (error) {
                                                                          NSLog(@"Error: %@", error.description);
                                                                      } else {
                                                                          NSLog(@"Success!");
                                                                      }
                                                                  }];
        isSuccessful = (appCall  != nil);
    }

    if (!isSuccessful) {
        [self performPublishAction:^{
            FBRequestConnection *connection = [[FBRequestConnection alloc] init];
            connection.errorBehavior = FBRequestConnectionErrorBehaviorReconnectSession
            | FBRequestConnectionErrorBehaviorAlertUser
            | FBRequestConnectionErrorBehaviorRetry;

            [connection addRequest:[FBRequest requestForUploadPhoto:image]
                 completionHandler:^(FBRequestConnection *innerConnection, id result, NSError *error) {
                     [self showAlert:@"Photo Post" result:result error:error];
                     if (FBSession.activeSession.isOpen) {
                         self.buttonPostPhoto.enabled = YES;
                     }
                 }];
            [connection start];

            self.buttonPostPhoto.enabled = NO;
        }];
    }
}

Sorry if this question seems too easy, but I am a newbie to Facebook SDK integration

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

1 Answers1

0

Generally you definitely want the user to be aware that something is being posted on their behalf, but to answer your question, if they've already granted you publish permissions, then you can use the code in the second "if" statement that you posted above, where it calls FBRequest requestForUploadPhoto:

Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • Hi @MingLi you seem to be pretty savvy when it comes to Facebook stuff, can you help me with Login in? I wrote the code but it's not working http://stackoverflow.com/questions/28843758/how-do-i-login-with-facebook-in-my-ios-app – Katedral Pillon Mar 04 '15 at 18:12