FBSessionNewPublishPermissions Request Fails Before Responding

I am trying to get work permissions from a user using the Facebook SDK on iOS.

I call the code below in the method, which is called if the application does not have the necessary permissions to publish for publication on 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 this code is called, the user goes to the permissions page, and before he even switches to safari on the device, the block is 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, 

Then the application continues to display the permissions page in safari, where the user selects ok. He then returns to the application. Permissions were not installed at this point, even if the user was granted the permissions page and accepted.

When you try to send a second time, the user goes to the permissions page in safari, and the requestNewPublishPermissions method is not interrupted instantly. The user selects ok, and then everything works as expected.

Thus, only for the first time, when calling the NewPublishPermissions request, he was not able to instantly return the ErrorReauthorizeFailedReasonUserCancelled error.

This happens in the simulator and on the device.

Any idea what could be causing this?

+6
source share
2 answers

I found a solution to this problem when answering this question Facebook iOS 3.1 sdk login with revocation permissions

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

Where opensessionforpublishpermissions is a method that contains the requestNewPublishPermissions method.

"The reason is that the reauthorize .. call must be after the event loop from which openActiveSession is called .."

I suppose this is a bug in the Facebook SDK, it makes no sense to be a normal behavior, and I did not see any of the comments on them on Facebook being expected.

+21
source

I had a similar problem and the answer provided by Tiddy worked for me. During some time.

Later I ran into the same problem. I don’t know why, maybe it was about SDK or iOS updates, the application cycle may be started, which has become more complicated. Therefore, I checked the source of the FB SDK and found out that this problem occurs when you request publication permissions immediately after read permissions , for example:

 // 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 application switches to Safari or FacebookApp and back, -application: openURL: sourceApplication: annotation: is called. Completion +openActiveSessionWithReadPermissions: immediately after that, applicationDidBecomeActive: And after the start of reauthorization, applicationDidBecomeActive: is finally called. So, the FB SDK believes that the user returned to the application without granting permissions , and the re-authorization failed with this error com.facebook.sdk 2. "error.

Sometimes dispatch_async() works well. But a reliable solution is to wait for an active session to process an application that has become active. And then request additional permission to publish. The following is an example of how to achieve this:

 - (void)applicationDidBecomeActive:(UIApplication *)application { [FBAppCall handleDidBecomeActive]; if (self.shouldReauthorise) { [self requestPublishPermissions]; self.shouldReauthorise = NO; } } 
+6
source

Source: https://habr.com/ru/post/946670/


All Articles