Search code examples
iosfacebookfacebook-ios-sdkfacebook-permissions

openActiveSessionWithPublishPermissions / requestNewPublishPermissions publish_actions not adding permission


Using SDK 3.22.0.

if (FBSession.activeSession.isOpen) {
    [FBSession.activeSession
     requestNewPublishPermissions:@[@"publish_actions"]
     defaultAudience:FBSessionDefaultAudienceFriends
     completionHandler:^(FBSession *session, NSError *error) {
         NSLog(@"error = %@", error);
         NSLog(@"session open = %d", session.isOpen);
         NSLog(@"session.permissions = %@", session.permissions);
         NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
     }];
}
else {
    [FBSession
     openActiveSessionWithPublishPermissions:@[@"publish_actions"]
     defaultAudience:FBSessionDefaultAudienceFriends
     allowLoginUI:YES
     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
         NSLog(@"error = %@", error);
         NSLog(@"session open = %d", session.isOpen);
         NSLog(@"status = %lu", status);
         NSLog(@"session.permissions = %@", session.permissions);
         NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
     }];
}

I'm testing with a user that doesn't have yet publish permissions and never declined it either. On FB Apps Settings on this account, the App Visibility is set to "Friends" and publishing permissions are not even on the settings list as they are for other users/apps.

In both cases of the code, the FB app opens and returns to my app immediately, without asking permissions. Response of declinedPermissions is an array with publish_actions.

My expectation is that FB app will ask the user to approve publishing.

I got reports from multiple users that experienced the same issue - not being able to add publish permissions, but some are able to get the permission.

One thing to add is that I had the same issue before submitting the app for FB approval with users outside of the test group, but when app got approved it started working for those users. Now it seems like the problem persists even when the app is approved, just for random users.

Am I doing anything wrong with the way I'm asking for permissions?


Solution

  • Looks like this worked:

    - (BOOL)hasWritePermissions {
        if (!FBSession.activeSession.isOpen) return NO;
        return [FBSession.activeSession.permissions indexOfObject:@[@"publish_actions"]] != NSNotFound;
    }
    
    - (void)requestWritePermissions:(void(^)(BOOL status, NSError *error))callback {
        if (self.hasWritePermissions) {
            callback(YES, nil);
            return;
        }
    
        if (FBSession.activeSession.isOpen) {
            [FBSession.activeSession
             requestNewPublishPermissions:@[@"publish_actions"]
             defaultAudience:FBSessionDefaultAudienceFriends
             completionHandler:^(FBSession *session, NSError *error) {
                 NSLog(@"error = %@", error);
                 NSLog(@"session open = %d", session.isOpen);
                 NSLog(@"session.permissions = %@", session.permissions);
                 NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
    
                 if (self.hasWritePermissions) {
                     callback(YES, nil);
                 }
                 else {
                     callback(NO, error);
                 }
             }];
        }
        else {
            [FBSession
             openActiveSessionWithPublishPermissions:@[@"publish_actions"]
             defaultAudience:FBSessionDefaultAudienceFriends
             allowLoginUI:YES
             completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                 NSLog(@"error = %@", error);
                 NSLog(@"session open = %d", session.isOpen);
                 NSLog(@"status = %u", status);
                 NSLog(@"session.permissions = %@", session.permissions);
                 NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
    
                 [self requestWritePermissions:callback]; // this time, with an open session
             }];
        }
    }
    

    If there's no session, I run openActiveSessionWithPublishPermissions and then run again requestNewPublishPermissions.

    Issue is that openActiveSessionWithPublishPermissions was firing the callback without even going to Facebook app for more permissions (looks like FB bug, will report), but this approach seems to solve it.

    Another issue I found is that session.permissions are not always reflecting the permissions on Facebook. The only way I found to ensure I have the latest permissions is to issue an API request:

    [FBRequestConnection startWithGraphPath:@"/me/permissions" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    

    and check the result.data array for granted/declined permissions.