Integrating ShareKit with Cocos2d for the iPhone

ShareKit is an awesome library for sharing almost any content with a number of social media sites. It’s incredibly powerful and can share images, URLs, text and even files. It’s smart enough to which social sites can accept which content and only show the services that can handle that content.

This was a really simple one but I wanted to share in case anyone else wants to combine ShareKit and Cocos2d for iPhone. First, follow the ShareKit instructions for installing and add it to your project. Now to display the sharekit action sheet, it’s dead simple:

In your .m file, include the sharekit sources:

#import "SHKItem.h"
#import "SHKActionSheet.h"

Then when you want to display the shareKit:

//Create your shared item, in this case it's the level score
SHKItem *item = [SHKItem text:[NSString stringWithFormat:@"I scored %i in %@ of Pond Hopper", levelFinalScore, [self getLevelGroupName:self.levelGroup]]];

//Create the sharekit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

//Show the action sheet in the openGLView
[actionSheet showInView:[CCDirector sharedDirector].openGLView ];

Simple as that.

5 Responses to “Integrating ShareKit with Cocos2d for the iPhone”

  1. Chuck
    November 4, 2010 at 11:36 am #

    Hi Shawn !
    Thanks for sharing the info.
    I did all the things you said, but i have a problem..
    I see the share screen, but when i click any of the buttons, it disappears..
    Thanks in advance…

    • Shawn Grimes
      November 4, 2010 at 11:51 am #

      Unfortunately, I’m not real good at troubleshooting unless I see some code. If you want, I can setup a dropbox so you can send me your code and I will take a look at it?

      • Chuck
        November 4, 2010 at 1:21 pm #

        That would be perfect ! let me prepare a clean proyect and i ll send it to you ! thanks again !

  2. Chuck
    November 7, 2010 at 5:04 pm #

    For cocos2d bellow v0.99.5-rc0
    use this:

    SHKItem *item = [SHKItem text:[NSString stringWithFormat:@”score “]];

    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    UIViewController *tempVC=[[UIViewController alloc] init];

    [[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];

    [[SHK currentHelper] setRootViewController:tempVC];

    [actionSheet showInView:[CCDirector sharedDirector].openGLView ];

  3. JC
    July 5, 2011 at 8:28 am #

    This single post just saved me hours thank you!

    My game is in landscape mode (cocos2d 0.99.5) but the actionSheet keeps popping up portrait from the right.

    How would one force it into landscape mode?

    Currently have no autorotation (other viewcontrollers in game all work fine like this).

    #define GAME_AUTOROTATION kGameAutorotationNone


Leave a Reply