我有一个分享链接的按钮。我基本上使用两个电话:
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];
}
}
如果在会话中没有找到权限,我请求发布权限
-(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
(我无法让它再次发生)。
处理这种情况的正确方法是什么?