FBSession SDK帮助程序调试

时间:2013-07-08 17:02:24

标签: ios facebook-ios-sdk

我正在尝试创建帮助程序来验证和请求Facebook的发布权限。 Facebook有一个非常混乱的API,例如[FBSession openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:],其中completionHandler不是你想的。每次会话状态更改时,completionHandler都会被混淆,而不是openActiveSession完成时。

我的流程很简单。问题是我偶尔得到那些常见的FBSession错误,比如FBSession: an attempt was made reauthorize permissions on an unopened session

有谁能告诉我我可能做错了什么?

- (void)openSessionWithBasicInfoThenRequestPublishPermissions:(void(^)(NSError *error))completionBlock
{
    [self openSessionWithBasicInfo:^(NSError *error) {
        if(error) {
            completionBlock(error);
            return;
        }

        [self requestPublishPermissions:^(NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionBlock(error);
            });
        }];
    }];
}

- (void)openSessionWithBasicInfo:(void(^)(NSError *error))completionBlock
{
    if([[FBSession activeSession] isOpen]) {
        completionBlock(nil);
        return;
    }

    self.openBlock = completionBlock;

    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self sessionStateChanged:session state:status error:error open:YES permissions:NO];
        });
    }];
}

static NSString *const publish_actions = @"publish_actions";

- (void)requestPublishPermissions:(void(^)(NSError *error))completionBlock
{
    if([[[FBSession activeSession] permissions] indexOfObject:publish_actions] != NSNotFound) {
        completionBlock(nil);
        return;
    }

    self.permissionsBlock = completionBlock;

    [FBSession.activeSession requestNewPublishPermissions:@[publish_actions] defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession *session, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self sessionStateChanged:session state:session.state error:error open:NO permissions:YES];
        });
    }];
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error open:(BOOL)open permissions:(BOOL)permissions
{
    if(self.openBlock && open) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.openBlock(error);
            self.openBlock = nil;
        });
    }
    else if(self.permissionsBlock && permissions) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.permissionsBlock(error);
            self.permissionsBlock = nil;
        });
    }
}

我对完成块的目标不是每次会话状态更改时调用它们,而是单次使用阻止我打开会话并在完成时在用户的墙上发帖。这就是为什么我将块存储在属性上然后在使用后将它们取出来。

0 个答案:

没有答案