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

FBSession requestNewPublishPermissions fails before response


I'm trying to get post permissions from a user using the Facebook SDK on iOS.

I'm calling the code below in a method that is called if the app does not have the required publishing permissions to post to the users facebook wall.

    // No permissions found in session, ask for it
    [FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:@"publish_actions"]
                                          defaultAudience: FBSessionDefaultAudienceEveryone
                                        completionHandler: ^(FBSession *session, NSError *error)
     {
         if( !error )
         {
          // Do something
         }
     }];

The first time I call this code it takes the user to the permissions page, and before it even switches to safari on the device the block gets called and this error message is returned

Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0xc426410 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorReauthorizeFailedReasonUserCancelled,

The app then continues on to show the permissions page in safari where the user selects ok. Then it returns to the app. Permissions have not been set at this point even tho the user was presented with the permissions page and accepted.

When trying to post a second time it takes the user to the permissions page in safari and the requestNewPublishPermissions method doesn't fail instantly. The user selects ok and then everything works as expected.

So it is only on the very first time calling requestNewPublishPermissions that it fails instantly returning the error ErrorReauthorizeFailedReasonUserCancelled.

This happens in the simulator and on the device.

Any idea what might be causing this?


Solution

  • I found the solution to this problem on the answer to this question Facebook iOS 3.1 sdk login with publish permission callbacks

     dispatch_async(dispatch_get_current_queue(), ^{
         [self openSessionForPublishPermissions];
     });
    

    Where opensessionforpublishpermissions is the method that contains the requestNewPublishPermissions method.

    "The reason is that the call to reauthorize.. needs to be after the event loop of which openActiveSession.. is called."

    I assume this is a bug in the Facebook SDK, it doesn't make sense for this to be normal behaviour and I haven't seen any of the Facebook docs comment on this being the expected behaviour.