Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS 3.1 sdk login with publish permission callbacks

I'm having trouble logging in with publish permissions in the facebook 3.1 ios sdk.

My app has a button to share a video, and when the user clicks it I want to add the basic + publish permission. As I understand, i have to do two calls -

  1. openActiveSessionWithReadPermissions, and then
  2. reauthorizeWithPublishPermissions

Here's the code I'm using now:

//Opens a Facebook session and optionally shows the login UX.
- (void)openSessionForReadPermissions
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {

         //this is called even from the reauthorizeWithPublishPermissions
         if (state == FBSessionStateOpen && !error)
         {
             [self openSessionForPublishPermissions];
         }
         else if (state == FBSessionStateClosedLoginFailed)
         {
             [FBSession.activeSession closeAndClearTokenInformation];

             [[NSNotificationCenter defaultCenter] postNotificationName:FBLoginErrorNotification object:session];
         }
     }];
}

-(void)openSessionForPublishPermissions
{    
    NSArray* permissions = [NSArray arrayWithObject:@"publish_stream"];

    [[FBSession activeSession] reauthorizeWithPublishPermissions:permissions
                                                 defaultAudience:FBSessionDefaultAudienceFriends
                                               completionHandler:
     ^(FBSession *session, NSError *error)
     {
         if (!error)
         {
             [[NSNotificationCenter defaultCenter]
              postNotificationName:FBLoginSuccessNotification
              object:session];
         }
         else
         {
             [[NSNotificationCenter defaultCenter]
              postNotificationName:FBLoginErrorNotification
              object:session];
         }
     }];
}

I see that the block in the openSessionForReadPermissions is called twice (once with FBSessionStateOpen and once with FBSessionStateOpenTokenExtended from the openSessionForPublishPermissions call), and I get a ErrorReauthorizeFailedReasonUserCancelled when first trying to login to the app (if O deleted all app permissions before).

What is the proper way to implement this login? The app does not require Facebook log-in, except for this one feature, so the login process should be on the same button push.

Thanks!

like image 691
iRadium Avatar asked Oct 16 '12 13:10

iRadium


1 Answers

I ran across this same issue. The solution I found was wrapping the call to [self openSessionForPublishPermissions]; in a dispatch_async block.

Example:

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

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

like image 148
Ray Morgan Avatar answered Oct 21 '22 04:10

Ray Morgan