Facebook iOS SDK 3.5.1:openActiveSessionWithReadPermissions - 完成处理程序调用两次 [英] Facebook iOS SDK 3.5.1: openActiveSessionWithReadPermissions - completion handler called twice

查看:111
本文介绍了Facebook iOS SDK 3.5.1:openActiveSessionWithReadPermissions - 完成处理程序调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享链接的按钮。我基本上使用两个电话:
openActiveSessionWithReadPermissions requestNewPublishPermissions

I have a button to share a link. I'm using basically two calls: openActiveSessionWithReadPermissions and 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];
        }
    }

我要求获得发布许可,如果在会话中未找到任何权限

and with this I'm asking for publish permission, if no permissione are found in 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只发贴一些东西

_share just posts something

-(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和app中登录过FB授权) openActiveSessionWithReadPermissions 的完成处理程序被调用两次:
一次使用FBSessionStateOpen,一次使用FBSessionStateOpenTokenExtended(来自openSessionForPublishPermissions调用)。
因此, _share 也会被调用两次,第一次在 else 部分 _prepareShare (如果我已经拥有发布权限),第二次在openSessionForPublishPermissions的完成处理程序中。
所以我在Facebook墙上有一个双重帖子,这是我第一次分享应用程序。我还有 FBSession的崩溃报告:在先前的重新授权呼叫尚未完成时重新授权是无效的(我无法再次发生这种情况) 。

First time I try to share (already logged on FB in iOS6 and app already authorized) completion handler of openActiveSessionWithReadPermissions is being called twice: once with FBSessionStateOpen and once with FBSessionStateOpenTokenExtended (from the openSessionForPublishPermissions call). As a consequence, _share is also called twice, first time in the else part of _prepareShare (if I already have publish permissions) and the second time in the completion handler of openSessionForPublishPermissions. So I have a double post on Facebook wall, just the first time I ever share in the app. I also had a crash report for FBSession: It is not valid to reauthorize while a previous reauthorize call has not yet completed (I couldn't be able to make it happen again).

处理这种情况的正确方法是什么?

What is the proper way to handle this situation?

推荐答案

看起来,通过设计,Facebook SDK保留了对块处理程序的引用,即使在它们被调用之后也是如此。因此,在您对 openActiveSessionWithReadPermissions 的调用中,如果会话状态发生更改,则可以多次调用完成处理程序。请参阅 Facebook此处对此问题的评论

It appears that by design, Facebook SDK retains references to block handlers, even after they have been called. Thus, in your call to openActiveSessionWithReadPermissions the completion handler may be called numerous times, in case the session state changes. See Facebooks comment on this issue here.

作为一种解决方法,您可能希望实现自己的机制,以确保只处理一次处理程序:

As a work around, you might want to implement your own mechanism that ensures the handler is fired only once:

__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;
                                      }

                                  }
     ];

这篇关于Facebook iOS SDK 3.5.1:openActiveSessionWithReadPermissions - 完成处理程序调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆