Posting on facebook with cocos2d

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

This is another quick tutorial, showing how to post / email what you need.
We will use the social.framework (don’t forget to add it to your library list, or you won’t be able to use
the SLComposeViewController.

facebook

I was using ShareKit (i should say trying to use) for all my needs (facebook, emails, tweeter, instagram etc….), the install setup is imho too heavy and time consuming. I also had several strange bugs like window not appearing every time (first button tap: no window, second: FB window appeared, then never appear….). Posting is usually some lines of code, i think it’s easier than using sharekit …

The code is pretty simple, it speaks by itself. The screenshot is taken as usual, using the ccrendertexture (see previous tutorial).

don’t forget to #import <Social/Social.h>

-(void) postFaceBook
{
    [[CCDirector sharedDirector] pause];
    [[CCDirector sharedDirector] stopAnimation];

    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

    UIImage * screenshot = [self takeScreenShot];

    SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [faceBookPost setInitialText:@"blablablabla"];
    [faceBookPost addImage:screenshot];
    [faceBookPost addURL:[NSURL URLWithString:@"http://www.yourlink.com"]];

    [[app navController] presentModalViewController:faceBookPost animated:YES];
    faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
    {
        [[CCDirector sharedDirector] resume];
        [[CCDirector sharedDirector] startAnimation];
        [[app navController] dismissModalViewControllerAnimated:YES];

    };
}

3 Comments

  1. A B C says:

    This is AWESOME.

    Thanks a lot. I needed this really bad. And this is by far the best of the bunch. The FaceBook API and ShareKit are a disaster right now.

    I would have paid you easily $100 for this. I was getting desperate. Andyway, great job. I appreciate. it.

  2. A B C says:

    …..And here is the corrected code for cocos2d 1.1. A slight change in the AppDelegate and Navcontroller is a viewcontroller.

    AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    CCScene *scene = [[CCDirector sharedDirector] runningScene];
    CCNode *_node = [scene.children objectAtIndex:0];
    UIImage *img = [self screenshotWithStartNode:_node];

    SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [faceBookPost setInitialText:@”blablablabla”];
    [faceBookPost addImage:img];
    [faceBookPost addURL:[NSURL URLWithString:@”http://www.yourlink.com”]];

    [[app viewController] presentModalViewController:faceBookPost animated:YES];
    faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
    {
    [[app viewController] dismissModalViewControllerAnimated:YES];

    };

    • lezard says:

      thanks for sharing this code too :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>