Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openActiveSessionWithReadPermissions does not log in automatically

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!!

like image 731
Jake Mogwai Avatar asked Nov 19 '12 17:11

Jake Mogwai


1 Answers

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.

like image 135
Alejandro Benito-Santos Avatar answered Nov 01 '22 09:11

Alejandro Benito-Santos