Cocos2D-X Facebook Integration

I’ve been working on ios facebook integration for cocos2D-X but I’ve run into a small problem.

I have put the ios Facebook SDK into the project and created a bridge for objective-C and C++. I’ve put all the facebook code inside methods in the app controller and I use the bridge to call them from Cocos2D-X files. So far I’ve gotten Facebook log in and permissions to work with no problem. My problem lies with retrieving user information.

This method is within the app controller which I call from the app delegate using the ios bridge. First it checks if the user is logged in and then it uses FBRequestConnection to retrieve the info.

-(NSString*)GetFacebookName
{
    name = @"";
    if (self.isLoggedInAfterOpenAttempt) {
        [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                               NSDictionary *user,
                                                               NSError *error) {
            if(error)
            {
                NSLog(@"Facebook name retrieval error");
            }
            else
            {
                name = user.name;
                NSLog(@"My name is %@", user.name);
            }
        }];
        return name;
    }
    else
    {
         return name;
    }
}

The problem is that the call back for this connection seems to only get executed after all other code has ran. This means that the function only returns an empty string to the app delegate, it then runs the rest of the code and then runs the call back.

This might be clearer from looking at the output:

Cocos2d: my name is:
2012-12-13 14:21:57.055 MyProjectl[64137:c07] My name is Keith Mccormac

First it printed out the CCLog inside the app delegate.
And then it prints out the NSLog from the method above.

It needs to be the other way around. Can anyone help with this problem or know a different way to go about doing this??

The call is asynchronous, so that function won’t block.
What I did is preset a loading dialog in my game and dismiss it when the function is completed.
I’ve used a singleton class that has a function I call when the request completes or fails.

Oren Bengigi wrote:

The call is asynchronous, so that function won’t block.
What I did is preset a loading dialog in my game and dismiss it when the function is completed.
I’ve used a singleton class that has a function I call when the request completes or fails.

Yeah i got it to work in the end using a singleton class. Bit messy but it works :slight_smile:
Thanks for the reply.