Search code examples
iosfacebooksessionsingle-sign-onfacebook-ios-sdk

Facebook "apprequest" dialog asks for facebook login where as app is already logged it to facebook


I'm following example of facebook authentication

https://developers.facebook.com/docs/tutorials/ios-sdk-games/authenticate/

for Login i've used:

 [FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES 
                                  completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                      if (session.isOpen) 
                                      {
                                          appDelegate.session=session; 

                                      }
                                  }//end completionHandler
         ];

and for facebook apprequest dialog:

 if (nil == appDelegate.facebook) 
{
    appDelegate.facebook = [[Facebook alloc]
                            initWithAppId:FBSession.activeSession.appID
                            andDelegate:nil];

    // Store the Facebook session information
    appDelegate.facebook.accessToken = FBSession.activeSession.accessToken;
    appDelegate.facebook.expirationDate = FBSession.activeSession.expirationDate;
}

[self.facebook dialog:APPREQUEST
       andParams:params
     andDelegate:self];

after some time when i try to run app again then app is connected to facebook and FBSession is also active, but when i call apprequest dialog then it asks for login.

is there any problem with appDelegate.facebook = [[Facebook alloc] or my session is expired?

Any suggestion please?


Solution

  • Try placing your Facebook object session setting code inside the completion handler. The completion handler should be called whenever the session changes, for example, later on when the token is extended, it should be called again. That way you can keep the Facebook object session refreshed. You should not have to extend the access token yourself, the SDK now does that automatically for you.

    [FBSession openActiveSessionWithPermissions:permissions 
                                   allowLoginUI:YES 
                              completionHandler:
        ^(FBSession *session, FBSessionState status, NSError *error) {
            if (session.isOpen) 
            {
                appDelegate.session=session; 
    
                if (nil == appDelegate.facebook) 
                {
                    appDelegate.facebook = [[Facebook alloc]
                            initWithAppId:FBSession.activeSession.appID
                            andDelegate:nil];
                }
    
                // Store the Facebook session information
                appDelegate.facebook.accessToken = FBSession.activeSession.accessToken;
                appDelegate.facebook.expirationDate = FBSession.activeSession.expirationDate;
            } // if session open
            else 
            {
                appDelegate.facebook = nil;
            } // session not open
        } //end completionHandler
    ];