我有一个共享链接的按钮。我基本上使用两个电话:openActiveSessionWithReadPermissions
和requestNewPublishPermissions
。
因此,这是按钮操作:
- (IBAction) shareFacebookButtonAction:(id)sender
if (![[FBSession activeSession] isOpen])
{
NSArray *permissions = @[@"read_friendlists", @"email"];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error)
{
if (FB_ISSESSIONOPENWITHSTATE([session state]))
{
[self _prepareShare];
}
else
{
// show alert view with error
}
}];
}
else
{
[self _prepareShare];
}
}
如果没有在 session 中找到被许可人,我就要求发布许可
-(void) _prepareShare;
{
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound)
{
[FBSession.activeSession
requestNewPublishPermissions:
[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error)
{
if (!error)
{
[self _share];
}
else
{
//error
}
}];
} else
{
[self _share];
}
}
_share刚刚发布了一些内容
-(void) _share;
{
NSMutableDictionary *params_dict = [NSMutableDictionary dictionary];
// setting some params
[FBRequestConnection startWithGraphPath:@"me/feed" parameters:params_dict HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (result)
{
// sharing succedeed, do something
}
else if (error)
{
//sharing failed, do something else
}
}];
}
第一次尝试共享(已在iOS6中登录FB并且应用已被授权)两次调用
openActiveSessionWithReadPermissions
的完成处理程序:一次使用FBSessionStateOpen,一次使用FBSessionStateOpenTokenExtended(来自openSessionForPublishPermissions调用)。
因此,
_share
也被调用两次,第一次是else
的_prepareShare
部分(如果我已经具有发布权限),第二次是在openSessionForPublishPermissions的完成处理程序中。因此,我第一次在该应用程序中分享时,我在Facebook墙上贴了一条双重帖子。我还收到了
FBSession: It is not valid to reauthorize while a previous reauthorize call has not yet completed
的崩溃报告(我无法使其再次发生)。处理这种情况的正确方法是什么?
最佳答案
根据设计,Facebook SDK似乎保留了对块处理程序的引用,即使它们已被调用也是如此。因此,在您对 openActiveSessionWithReadPermissions 的调用中,在 session 状态更改的情况下,可以多次调用完成处理程序。参见Facebooks comment on this issue here。
解决方法是,您可能需要实现自己的机制,以确保仅触发处理程序一次:
__block FBSessionStateHandler runOnceHandler = ^(FBSession *session,
FBSessionState status,
NSError *error) { /* YOUR CODE HERE */ };
...
[FBSession openActiveSessionWithReadPermissions:YOUR_PERMISSIONS
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (runOnceHandler) {
runOnceHandler(session, status, error);
runOnceHandler = nil;
}
}
];