Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FBSession requestNewPublishPermissions how to handle "Skip" user action

I found interesting issue.

When i'm requesting new publish permissions, fb sdk shows dialog with the "Skip" button. I'm handling it like this


        [[[appDelegate facebook] session] requestNewPublishPermissions:permissions
                                                       defaultAudience:FBSessionDefaultAudienceEveryone
                                                     completionHandler:^(FBSession *session, NSError *error)
         {
             if (!error)
             {
                  //do smth if there is no error
             }
         }];

But when user pressed "Skip" inside the dialog - error is nil.

How to handle it? Maybe i don't understand smth. Thx.

like image 319
Tunyk Pavel Avatar asked May 15 '13 17:05

Tunyk Pavel


1 Answers

same for me, seems like this is bug in Facebook SDK, but I am not sure. I have added additional check if session has new permissions.

    - (void)postToFacebookWithMessage:(NSString *)message completionBlock:(void (^)(NSError *))completionBlock
    {
        NSArray *permissions = @[@"publish_actions"];

        void (^completionHandler)(FBSession *session, NSError *error) = ^(FBSession *session, NSError *error) {
            if (error != nil)
            {
                if (completionBlock != nil)
                {
                    completionBlock(error);
                }
            }
            else if(![self activeSessionHasPermissions:permissions])
            {
                NSError *error = [NSError errorWithDomain:@"Facebook" code:FBAccessDisabledCode userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(@"Facebook access disabled", @"Facebook access disabled")}];
                if (completionBlock != nil)
                {
                    completionBlock(error);
                }
            }
            else
            {
                NSMutableDictionary<FBGraphObject> *action = [FBGraphObject graphObject];
                action[@"message"] = message;

                [FBRequestConnection startForPostWithGraphPath:@"me/og.posts" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                    // internal error 1611231 means that this was already posted
                    if (error.code == 5)
                    {
                        error = [NSError errorWithDomain:@"Facebook" code:FBDuplicateActionErrorCode userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(@"Already posted to Facebook", @"Already posted to Facebook")}];
                    }

                    if (completionBlock != nil)
                    {
                        completionBlock(error);
                    }
                }];
            }
        };

        if ([self activeSessionHasPermissions:permissions])
        {
            completionHandler([FBSession activeSession], nil);
        }
        else
        {
            [[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:completionHandler];
        }
    }

    - (BOOL)activeSessionHasPermissions:(NSArray *)permissions
    {
        __block BOOL hasPermissions = YES;
        for (NSString *permission in permissions)
        {
            NSInteger index = [[FBSession activeSession].permissions indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj isEqualToString:permission])
                {
                    *stop = YES;
                }
                return *stop;
            }];

            if (index == NSNotFound)
            {
                hasPermissions = NO;
            }
        }
        return hasPermissions;
    }

hope this will help

like image 167
evfemist Avatar answered Oct 18 '22 19:10

evfemist