Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 255
Tiddly Avatar asked Jun 06 '13 10:06

Tiddly


2 Answers

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.

like image 109
Tiddly Avatar answered Sep 25 '22 16:09

Tiddly


I had the similar issue, and the answer, provided by Tiddly worked for me. For some time.

Later I ran across the same issue. I don't know why, may be it was concerned with SDK or iOS updates, may be run loop of the app became more complicated. So I inspected FB SDK source and figured out that this issue occurs when you ask publish permissions just after read permissions, like this:

// Open with read permissions
[FBSession openActiveSessionWithReadPermissions:    readPermissions
                                   allowLoginUI:    YES
                              completionHandler:    ^
 (FBSession *session, FBSessionState status, NSError *error)
{
    // Ask for publish permissions (This is incorrect!)
    [FBSession.activeSession requestNewPublishPermissions:publishPermissions
                                          defaultAudience:FBSessionDefaultAudienceFriends
                                        completionHandler:
     ^(FBSession *session, NSError *error)
    {
        // ...
    }];
}];

When your app switches to Safari or FacebookApp and back, -application: openURL: sourceApplication: annotation: is called. CompletionHandler of +openActiveSessionWithReadPermissions: called immediately after this, before applicationDidBecomeActive:. And after you start reauthorisation applicationDidBecomeActive: is finally called. So, FB SDK think that user has returned back to the app, without giving permissions and reauthorisation fails with that "com.facebook.sdk error 2." error.

Sometimes dispatch_async() works well. But the robust solution, is to wait for active session to handle App Did Become Active event. And then request additional publish permissions. Here is an example of how to achieve this:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBAppCall handleDidBecomeActive];

    if (self.shouldReauthorise) {
       [self requestPublishPermissions];
       self.shouldReauthorise = NO;
    }
}
like image 37
kelin Avatar answered Sep 24 '22 16:09

kelin