Tutorial – Implement OpenFeint into Cocos2d Game

In this next tutorial we will implement OpenFeint into my new Cocos2d Game. If there is one thing you take from this blog, (besides the link to buy  PlanetOne) it is this implementation. OpenFeint is amazing.

So what is OpenFeint? OpenFeint is a service that enables your iPhone/iPod Touch/iPad application the ability to provide online score tracking.  The service is free and can easily be incorporated into your applications. Think XBOX Live meets Facebook for the iPhone with hundreds of games and millions of players.

One of the reasons I like OpenFeint is that it is an established network, with many users and games.

So lets get to it. This will take you less then 20min. probably. 🙂

Here is the Video (Typed out Instructions below):

First Step

Create an Account on OpenFeint and download the SDK.

Then import the “OpenFeint” folder into your XCode project.

Second Step

Right click on your project within XCode and select Get Info.

In looking at the build tab, and find Other Linker Flags, then give it the value -ObjC

Next find  ‘Call C++ Default Ctors/Dtors in Objective-C’ and put a check next to it.

Third Step

Import the nessecary Frameworks

Here is a list of the required ones:

  • Foundation
  • UIKit
  • CoreGraphics
  • QuartzCore
  • Security
  • SystemConfiguration
  • libsql3.0.dylib (located in (iPhoneSDK Folder)/usr/lib/)
  • CFNetwork
  • CoreLocation
  • MapKit (if building with SDK 3.0 or newer)

Here is the path where you can probably find them:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulatorx.3.0.sdk/System/Library/Frameworks/<select framework>

and the ‘libsql3.0.dylib’ can be found here:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulatorx.3.0.sdk/usr/lib/ libsql3.0.dylib

Fourth Step

add the following line to our prefix header:

#import “OpenFeintPrefix.pch”

Fifth Step

Next thing we need to do is create out OpenFeint Delegate to use with Cocos2d:

Create a new file “<yourgame>OFDelegate.mm/h”

add this to the header file:
#import "OpenFeintDelegate.h"
@interface <yourgame>OFDelegate : NSObject< OpenFeintDelegate >
- (void)dashboardWillAppear;
- (void)dashboardDidAppear;
- (void)dashboardWillDisappear;
- (void)dashboardDidDisappear;
- (void)userLoggedIn:(NSString*)userId;
- (BOOL)showCustomOpenFeintApprovalScreen;

Then add this to the main file:
#import "OpenFeint.h"
#import "<yourgame>OFDelegate.h"
#import "cocos2d.h"
@implementation <yourgame>OFDelegate
- (void)dashboardWillAppear

- (void)dashboardDidAppear
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];

- (void)dashboardWillDisappear

- (void)dashboardDidDisappear
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];

- (void)userLoggedIn:(NSString*)userId
OFLog(@"New user logged in! Hello %@", [OpenFeint lastLoggedInUserName]);

- (BOOL)showCustomOpenFeintApprovalScreen
return NO;


Sixth Step

Lets move over to your app delegate and add this to the main file:

#import "OpenFeint.h"
#import "SampleOFDelegate.h"

then find these methods and add the lines as necessary:

- (void)applicationWillResignActive:(UIApplication *)application {
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
[[CCDirector sharedDirector] pause];
[OpenFeint applicationWillResignActive]; // Add for OpenFeint

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
[OpenFeint applicationDidBecomeActive]; // Add for OpenFeint
- (void)applicationWillTerminate:(UIApplication *)application {
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
[[CCDirector sharedDirector] end];
[[NSUserDefaults standardUserDefaults] synchronize]; // Add for OpenFeint

Seventh Step

This will be the last thing.

Find a class where you want to implement OpenFeint…perhaps an intro scene before the main menu.

In that class you need to go to the header and add this:

@class <yourgame>OFDelegate; // Add for OpenFeint

@interface <your chosen class> : CCScene {
<yourgame>OFDelegate *ofDelegate; // Add for OpenFeint

Now Move over to the main file and add this:
#import "OpenFeint.h"
#import "<yourgame>OFDelegate.h"

And then in the init method add this:

NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation, [NSNumber numberWithBool:YES],
OpenFeintSettingDisableUserGeneratedContent, nil];
ofDelegate = [SampleOFDelegate new];
OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:ofDelegate];
[OpenFeint initializeWithProductKey:@"<yourOpenFeintKey>"
andDisplayName:@"My Crazy App"
andSettings:settings // see OpenFeintSettings.h
andDelegates:delegates]; // see OFDelegatesContainer.h



7 Responses to Tutorial – Implement OpenFeint into Cocos2d Game

  1. Pingback: Weekly Digest for May 30th — Hello. My name is Václav Vančura.

  2. chue says:

    is anyone else’s openfeint buggy? Mine is buggy. I set mine to portrait and landscape shows up only.

  3. neo says:

    Hi I have a problem when adding the openfeint in my project. when it had the sdk 4.0 worked perfectly but now with the sdk4.1 is giving errors even after following the tutorial. there is some compatibility problem with the SDK last? One interesting thing here was to put in tutorial a project sample with the cocos2d and openfeint already included.
    appreciate any help since I do not know what I can do.


  4. Beau Hankins says:

    For me it comes up with 222 errors and 2 warnings when I declare the ofDelegate. Can you tell me how to fix this

  5. Pingback: Getting Started With Cocos2D and OpenFeint | iPhone Game Tutorials

  6. John says:

    I am having issues with the orientation. It seems to draw from the top left instead of bottom left. So the openfeint dashboard seems to be rotated.

  7. John says:

    Sorted. had to include the openfeint.xml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: