8

我有一个分享链接的按钮。我基本上使用两个电话: openActiveSessionWithReadPermissionsrequestNewPublishPermissions.

所以这是按钮动作:

- (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(我无法让它再次发生)。

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

4

3 回答 3

13

从设计上看,Facebook SDK 似乎保留了对块处理程序的引用,即使在它们被调用之后也是如此。因此,在您调用openActiveSessionWithReadPermissions时,可能会多次调用完成处理程序,以防会话状态发生变化。请在此处查看Facebook 对此问题的评论

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

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

                                  }
     ];
于 2013-06-19T17:51:34.067 回答
2

你可以用这个

- (IBAction)facebookBasti:(id)sender {
if(FBSession.activeSession.isOpen){

    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
        if (!error) {

            NSLog(@" Email = %@",[user objectForKey:@"email"]);
        }
    }];

    NSLog(@"POST TO WALL -- %@",FBSession.activeSession.accessToken);
    [self publishFacebook];

}
else {
    // try to open session with existing valid token
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"publish_actions",@"email",
                            nil];
    FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
    [FBSession setActiveSession:session];
    if([FBSession openActiveSessionWithAllowLoginUI:NO]) {
        // post to wall
        [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
            if (!error) {

                NSLog(@" Email = %@",[user objectForKey:@"email"]);
            }
        }];

        NSLog(@"POST TO WALL -- %@",FBSession.activeSession.accessToken);
        [self publishFacebook];
    } else {
        // you need to log the user
        NSLog(@"login");

        [FBSession openActiveSessionWithPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState state,
                                                      NSError *error) {
                                      NSLog(@"POST TO WALL -- %@",FBSession.activeSession.accessToken);
                                      [self publishFacebook];

                                  }];
    }
}

}

并发布Facebook方法

   -(void)publishFacebook
   {
NSMutableDictionary *postParams2= [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                   haberLink, @"link",
                                   @"abc.com", @"name",
                                   title, @"caption",
                                   desc, @"description",
                                   nil];

[FBRequestConnection
 startWithGraphPath:@"me/feed"
 parameters:postParams2
 HTTPMethod:@"POST"
 completionHandler:^(FBRequestConnection *connection,
                     id result,
                     NSError *error) {
     NSString *alertText;
     if (error) {
         alertText = [NSString stringWithFormat:
                      @"error: domain = %@, code = %d",
                      error.domain, error.code];
     } else {
         alertText = [NSString stringWithFormat: @"Shared Facebook"];



         [[[UIAlertView alloc] initWithTitle:@"Shared Facebook"
                                     message:alertText
                                    delegate:self
                           cancelButtonTitle:@"Ok"
                           otherButtonTitles:nil]
          show];

     }
 }];

}

于 2013-05-22T08:30:44.657 回答
1

请阅读从 3.0 升级到 3.1,特别是分别请求读取和写入权限的段落。似乎 Facebook SDK 不适合以这种方式使用。

您现在需要单独(并按此顺序)请求阅读和发布权限。最有可能的是,当应用程序启动并且用户首次登录时,您将请求读取权限以进行个性化。稍后,如果合适,您的应用程序可以在打算将数据发布到 Facebook 时请求发布权限。

重要的是,您不要简单地尝试背靠背调用这两个单独的方法来替换任何一个已弃用的函数。

我想知道你是如何设法解决这个问题的。顺便说一句,我得到了相同的崩溃报告(FBSession:重新授权无效,而先前的重新授权调用尚未完成)。

于 2013-11-06T11:16:24.487 回答