1
votes

Facebook SDK 3.2

I need to post a wall post on facebook just after sign-in with faceboook.

Here is the Code i am using:

Here i have a opened the Session For Facebook:

- (IBAction)FBSignup:(id)sender {
    [[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) {
        [self sessionStateChanged:session state:state error:error];
    }];
}

On State Change, I am populating Data with [self populateUserDetails] and then trying to post on facebook wall as well with [self postOnFB] but there is no post on facebook wall.

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:

            [self populateUserDetails];
            [self postOnFB];
            break;

        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:SCSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

Here i am populting data with the facebook user object where i need to.

- (void)populateUserDetails
{
    if (FBSession.activeSession.isOpen) {

        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {

             if (!error) {
               ///////////////////////////////  
               // Populating Data Where Needed
               ///////////////////////////////


              //   [self postOnFB];
             }
         }];
    }
}

Here i try to post the wall post with [self postOnFB] it gives error:

Terminating app due to uncaught exception 'com.facebook.sdk:InvalidOperationException', reason: 'FBSession: It is not valid to reauthorize while a previous reauthorize call has not yet completed.

Here i am posting to the wall.

- (void)postOnFB{

    // Ask for publish_actions permissions in context
    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) {
                                                    [self fbPostSettings];
                                                }
                                            }];
    } else {
        // If permissions present, publish the story
        [self fbPostSettings];
    }
}

setting parameters for facebook wall post:

- (void)fbPostSettings{

NSDictionary * facebookParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     kFacebookPostLink,                             @"link",
     kFacebookPostImage,                            @"picture",
     kFacebookPostName,                             @"name",
     kFacebookPostCaption,                          @"caption",
     kFacebookPostDescription,                      @"description",
     nil];

    [FBRequestConnection startWithGraphPath:@"me/feed"
                                 parameters:facebookParams
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
                              NSString *alertText;
                              if (error) {
                                  alertText = [NSString stringWithFormat:
                                               @"error: domain = %@, code = %d",
                                               error.domain, error.code];
                              } else {
                                  alertText = [NSString stringWithFormat:
                                               @"Posted action, id: %@",
                                               [result objectForKey:@"id"]];
                              }
                              NSLog(@"%@",alertText);
                          }];
}

- (void)sessionStateChanged:(NSNotification*)notification {
    [self populateUserDetails];
}

If i call postOnFB on some button action selector (after completion of data population through facebook user object), it post fine on the wall. But i need to post it just after i get the user object in startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary *user, NSError *error) method.

Please help. Thank you everyone :)

1

1 Answers

6
votes

Instead of opening session and then requesting publish permissions, directly ask for publish permissions to open the session

[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_stream",
                                                    nil] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, 
   FBSessionState state, NSError *error) {
if (FBSession.activeSession.isOpen && !error) {
     [self fbPostSettings]; 
}];