openActiveSessionWithReadPermissions does not log in automatically

强颜欢笑 提交于 2019-12-21 12:20:08

问题


I have recently started using Facebook SDK 3.1, and am encountering some problems with logging in using openActiveSessionWithReadPermissions.

Actually, loggin in works perfectly, if there is a cached token available, it logs in without presenting Facebook UI, and if not, it presents the UI.

The problem occurs after I make a call to reauthorizeWithPublishPermissions. If I call reauthorizeWithPublishPermissions, then close and reopen the application, and make a call to openActiveSessionWithReadPermissions, it presents the Facebook UI and requires the user to say "yes I'm OK with read permissions", even though there is a cached token available.

It only presents the Facebook UI erroneously if I make a call to reauthorizeWithPublishPermissions, otherwise everything works fine.

Open for read code:

[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);
                }];
            }
        }];

the sessionStateChanged function:

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

the Publish call, with an empty aPublishAction for testing:

- (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);
    }
}

Thanks for everything in advance, I would be grateful for any and all help!!


回答1:


You need to add

[FBSession.activeSession handleDidBecomeActive];

to your -(void) applicationDidBecomeActive:(UIApplication *)application method in your app's delegate as stated in the migration guide.




回答2:


What if you use openActiveSessionWithPermissions instead of openActiveSessionWithReadPermissions?



来源:https://stackoverflow.com/questions/13458597/openactivesessionwithreadpermissions-does-not-log-in-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!