Integrating Apple GameCenter to your iPhone Application

Step 1: Create a AppID on Provisional Portal -( http://developer.apple.com/ios)

  • Make a new AppId and make sure you spell out the bundle identifier properly so game center can be enabled for it for eg: com.vivianaranha.whofarted



  • Once you do this GameCenter is enabled by Default

Step 2: Create an Application on iTunes Connect and configure GameCenter for the App

  • Click on Manage Game Center


  • Click on Enable button to enable GameCenter for this app


  • You will see 2 sections now
  1. LeaderBoard, and
  2. Achievements
  • Go ahead and click on Setup for LeaderBoard, we will do Achievements once we setup LeaderBoard

  • You will see the following screen – Go ahead ans select Ascending or Descending depending on your Leaderboard order
  • Select the Format type depending on your game play, it can be integer, decimal, time or money
  • Once you select this Add category and Score Format Localization will be active.

  • Click on Add Category and you will see a modal window popup. It has 2 fields, you will be using the categoryID to access the leaderboard.
  • Category Reference Name:¬† Test LeaderBoard
  • Leaderboard Category ID: testleaderboard
  • Hit Save
  • IMPORTANT – The app uses the Bundle ID and the leaderboard category together to access the leaderboard from GameCenter.

  • Next click on Add Language and a modal popup will show up to add a language. These are self explanatory
  • Language: Select Language
  • Score Format: Select the one that applies
  • Score Format Suffix (Singular): you can add things liks point or say Dollar or say cent or sec…. depending on your game or leave empty if you dont want any suffix
  • Score Format Suffix (Plural): Same thing as above but for plural
  • Category Title for Test LeaderBoard – What Title you want for this particular Category – It will show up on top of the leaderboard navigation bar
  • Hit save will close the popup

  • Hit Save and thats it for leaderboard.
  • Now its time to add an Achievement – Click on setup button under Achievements
  • You see the following screen

  • You have 1000 points available to add achievements – you can use them however you want, 1 per achievement or 100 per achievement¬† – it all depends on your app and you
  • Its not related to your leaderboard in anyway, do not confuse these points with leaderboard points. But you can combine them in your code, its totally upto you how you manage your achievements and leaderboard.
  • Click on Add new Achievement to get started.
  • You will see a form – Fill it up and remember you will reference it from your app using Achievement ID you add here

  • Add Language will popup a form
  • Add details about title, what will be displayed before the achievement is earned and what will show up once the achievement is earned.
  • There is a image too which shows up only once the achievement is earned, if you have seen on gamecenter apps, they will have a ? mark first and when you get that achievement you see an image – this is that image.

  • Hit save on it and then save again to save achievements.

Step 3: Xcode – Create a new app where You will be implementing the above

Step 3a: Implementing leaderboard

  • Open Xcode – New Project – Select View-Based Application

  • I called my app “WhoFarted”

  • Add the GameKit Framework to your Frameworks (right click on Frameworks folder – click on Add existing frameworks – and Pick GameKit.framework)

  • Go to WhoFarted-Info.plist file( your info.plist file) and in Bundle Identifier add your identifier you created using your provisional profile appid – com.vivianaranha.whofarted

  • Next go to ViewController h file (WhoFartedViewController.h) and add the following
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface WhoFartedViewController : UIViewController {

	IBOutlet UILabel *lblScore;
	int *score;
}

-(IBAction)addScore;
-(IBAction)submitMyScore;
-(IBAction)showLeaderboard;

@end
  • Next go to .m file (WhoFartedViewController.m) and add the following
#import "WhoFartedViewController.h"

@implementation WhoFartedViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){
		if (error ==nil) {
			NSLog(@"Success");
		} else {
			NSLog(@"Fail");
		}

	}];
}

-(IBAction)addScore{
	score += 10;
	NSString *scrVal = [NSString stringWithFormat:@"%d",score];
	lblScore.text = scrVal;
}

-(IBAction)submitMyScore{
	//This is the same category id you set in your itunes connect GameCenter LeaderBoard
	GKScore *myScoreValue = [[[GKScore alloc] initWithCategory:@"testleaderboard"] autorelease];
	myScoreValue.value = score;

	[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
		if(error != nil){
			NSLog(@"Score Submission Failed");
		} else {
			NSLog(@"Score Submitted");
		}

	}];
}

-(IBAction)showLeaderboard{
	GKLeaderboardViewController *lb = [[GKLeaderboardViewController alloc] init];
	if(lb != nil){
		lb.leaderboardDelegate = self;
		[self presentModalViewController:lb animated:YES];
	}
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
	[self dismissModalViewControllerAnimated: YES];
	[viewController release];
}

@end
  • Next open WhoFartedViewController.xib and make it so that it looks like this

  • Build and Run the project – it should be working fine, if you have followed through every step.
  • Congrats, you have your LeaderBoard Implemented.

Step 3b: Implementing Achievements

  • We have already added achievements into our iTunes Connect in the previous step¬† – Lets go ahead and first show it to the user through a button.
  • Go to View Controller .h file (WhoFartedViewController.h) and add the following
- (IBAction) showAchievements;
  • Add the corresponding function into the .m file
- (IBAction) showAchievements
{
	GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
	if (achievements != NULL)
	{
		achievements.achievementDelegate = self;
		[self presentModalViewController:achievements animated: YES];
	}
}

- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController;
{
	[self dismissModalViewControllerAnimated: YES];
	[viewController release];
}
  • Now lets go to the .xib file and add the UIButton and link it as shown

  • Build and Run your code and you will see achievements show up when click on the Achievements button – Simple as that.
  • Now to achieve the achievement – You might have to write some code – So say for this case I can say as soon as the user submits more then 1000 points – I will check and give him the achievement.
  • The code for this -
  • Add this to the bottom of your submitscore function
[self checkAchievements];
  • So we have to write this method checkAchievements to check and grant achievements. Here it is add to the .m file
 (void) checkAchievements
{
	if(score > 1000){
		GKAchievement *achievement= [[[GKAchievement alloc] initWithIdentifier:@"testachievement"] autorelease];
		achievement.percentComplete = 100.0;
		if(achievement!= NULL)
		{
			[achievement reportAchievementWithCompletionHandler: ^(NSError *error){
				if(error != nil){
					NSLog(@"Achievement failed");
				} else {
					NSLog(@"Achievement Success");
				}

			 }];
		}
	}
}
  • Build and Run and wallah its all good. There you go you have just implemented GameCenter LeaderBoard and Achievements.
  • Here is a link to the code – Download

Leave a Reply