iOS Facebook网络共享API - 如何判断用户是否已跳过

时间:2014-01-21 15:48:35

标签: ios objective-c facebook error-handling

我有一个带有按钮的VC,可以在facebook上分享一些文字和图片网址。

我使用FB api打开FB webview - 供用户登录。然后它显示共享窗口,显示“跳过”或“共享”按钮...

enter image description here

我在我的应用程序中有以下代码来处理登录和按钮操作.... 出于某种原因,“Skip”也会发布,好像“OK”被击中一样。我尝试使用错误实用程序返回的cheking thr错误代码,但这不起作用(应用程序永远不会进入该部分代码,并且“CANCELED OUT”永远不会记录在NSLog中,即使我点击“跳过”按钮)

对此有任何帮助将非常感激。也许某种方式来重新检查权限?

(void)postStatusUpdate:(id)status
{
    if (FBSession.activeSession.state != FBSessionStateCreatedTokenLoaded || [FBSession.activeSession.permissions
                                                                              indexOfObject:@"publish_actions"] == NSNotFound) {
        // Permission hasn't been granted, so ask for publish_actions
        [FBSessionopenActiveSessionWithPublishPermissions:@[@"publish_actions"]
                   defaultAudience:FBSessionDefaultAudienceFriends
                        allowLoginUI:YES
                        completionHandler:^(FBSession *session, FBSessionState state, 
                                            NSError *error) {
                             [selfsessionStateChanged:session state:state error:error];

                              if([FBErrorUtilityerrorCategoryForError:error] ==
FBErrorCategoryUserCancelled)
                              {
                                   NSLog(@"SNFacebookClient: CANCELLED OUT!!");
                              }
                              if (FBSession.activeSession.isOpen && !error) {
                                   // Publish the story if permission was granted
                                   [selfpublishStory:status];
                              }
         }];
    } else {
        // If permissions present, publish the story
        [selfpublishStory:status];
    }
}

代码

我甚至尝试在lldb中按下“跳过”后检查权限数组...

po [FBSession activeSession].permissions 

并发现即使按下skip,数组也包含条目“publish_actions”

1 个答案:

答案 0 :(得分:1)

您需要检查会话状态,如果状态为FBSessionStateOpenFBSessionStateCreatedTokenLoaded,您可以获取用户的数据。 否则,会发生错误或用户跳过它。

这种方法与您不同,请查看我的简短评论,了解相关步骤。

    // Suppose my FBSession is stored in a property called session
    @property (nonatomic, strong) FBSession *session;

    // The session getter can be implemented like this

    - (FBSession *)session
    {
        if(!_session)
        {
            _session = [[FBSession alloc] initWithPermissions:readPermissions];
            [FBSession setActiveSession:_session];
        }

        return _session;
    }



    // Opening a new session 
    [self.session openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            [self session:session stateChanged:status error:error];
        }];


    - (void)session:(FBSession *)session stateChanged:(FBSessionState)state error:(NSError *)error
    {
        switch(state)
        {
            case FBSessionStateOpen:
            {
                // Connection accepted
            }
            break;

            case FBSessionStateCreatedTokenLoaded:
            {
                // Connection restored
            }
            break;

            case FBSessionStateClosed:
                // Connection closed
                break;

            case FBSessionStateClosedLoginFailed:
            {
                // Connection failed and closed
            }
            break;

            case FBSessionStateCreated:
            {
                // Session created (not opened yet)
            }
            break;

            case FBSessionStateCreatedOpening:
                // Handler it if you need to do something while connection is opening
                break;

            case FBSessionStateOpenTokenExtended:
                // Session token extended
                break;
        }
    }