6

我最近开始使用 Facebook SDK 3.1,在使用openActiveSessionWithReadPermissions.

实际上,login in 工作得很好,如果有可用的缓存令牌,它会在不显示 Facebook UI 的情况下登录,如果没有,它会显示 UI。

在我拨打reauthorizeWithPublishPermissions. 如果我调用reauthorizeWithPublishPermissions,然后关闭并重新打开应用程序,然后调用openActiveSessionWithReadPermissions,它会显示 Facebook UI 并要求用户说“是的,我可以读取权限”,即使有可用的缓存令牌。

如果我拨打电话,它只会错误地显示 Facebook UI reauthorizeWithPublishPermissions,否则一切正常。

打开阅读代码:

[FBSession openActiveSessionWithReadPermissions:readpermissions allowLoginUI:YES
                                         completionHandler:^(FBSession *aSession, FBSessionState status, NSError *error) {
            [self sessionStateChanged:[FBSession activeSession] state:status error:error];
            if (status != FBSessionStateOpenTokenExtended) {
                // and here we make sure to update our UX according to the new session state
                FBRequest *me = [[FBRequest alloc] initWithSession:aSession
                                                         graphPath:@"me"];
                [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                                 NSDictionary<FBGraphUser> *aUser,
                                                 NSError *error) {
                    self.user = aUser;

                    aCompletionBlock(aSession, status, error);
                }];
            }
        }];

sessionStateChanged 函数:

- (void)sessionStateChanged:(FBSession *)aSession state:(FBSessionState)state error:(NSError *)error {

    if (aSession.isOpen) {
        // Initiate a Facebook instance and properties
        if (nil == self.facebook || state == FBSessionStateOpenTokenExtended) {
            self.facebook = [[Facebook alloc]
                             initWithAppId:FBSession.activeSession.appID
                             andDelegate:nil];

            // Store the Facebook session information
            self.facebook.accessToken = FBSession.activeSession.accessToken;
            self.facebook.expirationDate = FBSession.activeSession.expirationDate;
        }
    } else {
        // Clear out the Facebook instance
        if (state == FBSessionStateClosedLoginFailed) {
            [FBSession.activeSession closeAndClearTokenInformation];
        }
        self.facebook = nil;
    }
}

Publish 调用,带有一个空的 aPublishAction 用于测试:

- (void)doPublishAction:(void(^)(FBSession *aSession, NSError *error))aPublishAction {

    if ([FBSession.activeSession.permissions
         indexOfObject:@"publish_actions"] == NSNotFound) {

        NSArray *writepermissions = [[NSArray alloc] initWithObjects:
                                     @"publish_stream",
                                     @"publish_actions",
                                     nil];

        [[FBSession activeSession]reauthorizeWithPublishPermissions:writepermissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *aSession, NSError *error){
            if (error) {
                NSLog(@"Error on public permissions: %@", error);
            }
            else {
                aPublishAction(aSession, error);
            }


        }];

    }
    else {
        // If permissions present, publish the story
         aPublishAction(FBSession.activeSession, nil);
    }
}

提前感谢您所做的一切,我将不胜感激任何和所有的帮助!

4

2 回答 2

6

您需要添加

[FBSession.activeSession handleDidBecomeActive];

如迁移指南-(void) applicationDidBecomeActive:(UIApplication *)application中所述,添加到您的应用程序委托中的方法。

于 2012-11-20T17:18:18.567 回答
-2

如果您使用 openActiveSessionWithPermissions 而不是 openActiveSessionWithReadPermissions 会怎样?

于 2012-11-26T09:45:42.740 回答