Facebook SDK: Request Read&Publish permissions simultaneously

Facebook says:

You cannot request read and publish permissions simultaneously using iOS6's Facebook support, and if you attempt to, the SDK will take the user through a less-optimal app-switching or web-based authentication flow.

If all you need is to authorize your iOS application and grant publish_actions permission, and if you really want integrate with iOS system defined Facebook account, there is a trick you need to do:

You NEED to ask for reading permission before you ask for publishing permission - this is a need, there is no way around. Two-step flow is required because you can't ask for publishing permissions for the first time.

Here is my solution:

First of all, you may find convienient helper method to verify if you granted all required permissions

+ (BOOL) checkFacebookPermissions:(FBSession *)session
{
    NSArray *permissions = [session permissions];
    NSArray *requiredPermissions = @["publish_actions"];

    for (NSString *perm in requiredPermissions) {
        if (![permissions containsObject:perm]) {
            return NO; // required permission not found
        }
    }
    return YES;
}

with this magic sequence, user will be asked (for the first time) for reading permission and for publishing permission, right away:

if (!FBSession.activeSession.isOpen || ![self checkFacebookPermissions:FBSession.activeSession]) {  
    // first check if this is first time (no read permissions yet)
    [FBSession openActiveSessionWithReadPermissions:@["email"] allowLoginUI:YES completionHandler:^(FBSession *session1, FBSessionState status, NSError *error) {
        if (session1.state == FBSessionStateOpen) {
            // then ask for publishing permission
            if (![self checkFacebookPermissions:FBSession.activeSession]) {
                [session1 requestNewPublishPermissions:@["publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session2, NSError *error) {
                    if (session2.state == FBSessionStateOpenTokenExtended) {
                        // THIS IS IT, THE GLORY FINISH
                    }
                }];
            }
        }
    }];
}

I don't know if this is the most optimal solution, but this may save you some time digging what is wrong with you facebook integration.

This post has been previously published on Coderwall.com

PS. Cover photo: https://www.flickr.com/photos/dkalo/4815259737