Flixel Forums

development => iOS => Topic started by: mutantandy on Fri, Jul 6, 2012

Title: Facebook integration
Post by: mutantandy on Fri, Jul 6, 2012

So I've just implemented Facebook publishing to my news feed in my game, thought I would share the way I did it.  I'm not sure if it's the perfect way, but it seems to have the desired effect.

To start, follow the instructions on developer.facebook.com on setting up your app and add the Facebook iOS SDK files to your project.   Then to make it work in the game, see below.

One of my main issues was, I wanted to avoid having people sign in to Facebook as soon as the app opens, and would rather people only have to sign in if they want to post their score to Facebook.  So I put the initialisation code for Facebook in the FlxGame class.  In the header file add the properties:

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) FlashFunction *facebookCallbackFunction;

And add the function:

-(void)initFacebookWithCallback:(FlashFunction *)flashFunc;

Then in the main class add the following functions:

-(void)initFacebookWithCallback:(FlashFunction *)flashFunc
    facebookCallbackFunction = [flashFunc retain];
    facebook = [[Facebook alloc] initWithAppId:@"YOUR-APP-ID" andDelegate:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults objectForKey:@"FBAccessTokenkey"] && [defaults objectForKey:@"FBExpirationDateKey"])
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    NSArray *permissions = [[NSArray alloc] initWithObjects:@"read_stream", @"publish_stream", nil];
    if(![facebook isSessionValid])
        NSLog(@"User has not previously logged in");
        [facebook authorize:permissions];
        [permissions release];
        NSLog(@"User previously logged in");

    NSLog(@"Facebook did login successfully");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
    [facebookCallbackFunction execute];

There's a bunch of other functions you need as part of the FBSessionDelegate and FBRequestDelegate protocols.  I've just got these logging out when they run at the moment but will flesh out down the line.

OK, so that's the FlxGame class stuff done.  There's another little bit of housekeeping you need to do before it'll do anything.  In the AppDelegate, put the following functions:

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
    return [[game facebook] handleOpenURL:url];

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    return [[game facebook] handleOpenURL:url];

Next we need to run the Facebook functions and publish things.  So I did this in my end-game screen so users can publish the scores to their feed.  Add your share to wall button in the create function (or whenever you want to initialise it) with the following code:

shareOnFacebookButton = [[FlxButton alloc] initWithX:130.0f y:255.0f callback:[FlashFunction functionWithTarget:self action:@selector(fbShareHandler)]];
[shareOnFacebookButton loadGraphic:[FlxSprite spriteWithGraphic:@"facebook_btn.png"]];
[self add:shareOnFacebookButton];

And add these functions to publish it things:


    //This line starts up Facebook
    [[FlxG game] initFacebookWithCallback:[FlashFunction functionWithTarget:self action:@selector(postScoreToWall)]];

    //This line will run the function that publishes to you wall
    [self postScoreToWall];

//This function publishes the score to your wall
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"My test app", @"name",
                                   @"http://google.com", @"link",
                                   @"FBTest app for iPhone!", @"caption",
                                   @"This is a description of my app", @"description",
                                   @"Hello this is a test message", @"message",
    [[[FlxG game] facebook] requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:[FlxG game]];

Then give it a test, hopefully that all works.  This is the way I did it.  If anyone knows a better way I'd gladly hear it.
Title: Re: Facebook integration
Post by: initials on Fri, Jul 6, 2012

I had a lot of trouble with any social media services and Flixel. This is a great help.
Title: Re: Facebook integration
Post by: tokyotron on Fri, Jul 6, 2012
I think it's already official that there's a new framework in iOS 6 that abstracts FB and similar platforms into one API...  Like Twitter and a Chinese SNS. Imagine your sales when 2 billion Chinese people chat online about your game. And oh FB is banned in China, and wait, maybe Twitter too :-)
Title: Re: Facebook integration
Post by: mutantandy on Mon, Jul 9, 2012
Ah cool!  I knew about the Facebook integration stuff, but I didn't know about the Chinese SNS integration too.  That's great.  Looking forward to the official release of iOS 6 even more now :)