Email: info@olbuz.com    |     Phone: +91 6353967672    Follow us:
Email: info@olbuz.com    |     Phone: +91 6353967672    Follow us:

Learn to Integrate Game Center in iOS Project – Apple’s Social Gaming Network

gamecenter cover image

Game Center is a social gaming network owned by Apple Inc. that supports online multi-player gaming. IOS users can invite in their friends into a game via matchmaking. The Game Center dashboard further allows the users to compare their scores with the leader board and track their achievement. Some games can even share multiplayer functionality between iOS and Mac versions. Originally introduced in April 2010, the service has been going through several updates and is now supported on iOS 4.1 and the above version of OS and can be integrated via GameKit framework.  If you are developing a game and looking for huge exposure, integration of Game Center is recommended.

To understand the concept of Game Center one should highly focus on its ‘social’ aspects. To be able to support the social features, the platform allows developers to manage player data in the form of images and text. Developers will need to have an iTunes Connect account wherein they will be adding the Game Center configuration details. Be i share technical information, lets go through some of the important features supported by Apple Game Center.

Features supported and provided by Game Center

It’s so much like any other social networking platform. Using the Game Center, players can seek to connect with new friends by sending friend requests, search for friends, organize multiplayer games online and start their engagement in the virtual competitive environment. Game Center allows for a maximum of 500 connections (friends). Now, depending upon the game, the dashboard will feature achievements and points won. The leader board also allows a user to compare his/her score with other players from across the globe playing the game.

Several iOS games completely depends on Game Center to provide multiplayer gaming experience. It is completely upon the developer whether they choose to use all or some of the various features provided through Game Center.

Here are the several components as described in iTunes Connect:

  1. Leader boards – It’s a sort of database that collects and ranks scores of players for each game in the Game Center. Developers can use iTunes connect for configuring the GameCenter scores, the method of ranking and the display in a language supported by the app.
  2. Leader board sets – These help organize the leader board by limiting the ranking to a certain amount. Developers can use this feature to expand the default leader board of 100 to 500 or display several sets (even 100) of leader board.
  3. Achievements – This lists all the unique achievements that the game is supposed to present. Using iTunes Connect, developers can choose to define the number and language of the achievements.
  4. Game Groups – This feature allows developers to define achievements and leader boards for ranking players from different games, pay levels and platforms. When adding to the group, developers can define the exact achievements and leader boards to be included into the group.
  5. Multiplayer compatibility lists – Helps identify different versions of the same game. This is also the tool to identify compatible games that can be simultaneously played. The lists have to be configured into iTunes.
  6. Other features like Challenges and Matchmaking don’t need to be configured into iTunes Connect.

As we are not familiar with all important features of Apple game center, we we move with the actual integration part. Integrating games into the Game Center isn’t that hard but developers must follow a two-step process.

  • Firstly, developers need to create a new app record on iTunes Connect. This enables/activates the Game Center support for features like leader boards and achievements.
  • Secondly, the project needs to integrate all the GameKit frameworks / libraries that make it compatible with the Game Center.

Lets being with first part of the integration:

Step 1 

Login to your iTunes connect account and create new iOS app by inserting all require information. Once you are done, click on Game Center tab and you will land on below screenshot. Now click on enable for single or group of game as per your requirement.

GameCenter_1

You will see below screen shows your Game center is enabled now.

GameCenter_2

Once you are done, follow instructions and click on Add Leaderboard; you will see below screen. Now choose the Single leaderboard as we have enabled the Game center of single game.

AddLeaderBoard_1
Insert requested information as per below screenshot and click on Add Language.

AddLeaderBoard_2
Now you are done with Leaderboard creation as shown in below screenshot.

GameCenter_3
Once the leaderboard creation is done, the next part is to create the Achievement by following steps as shown in below screenshot.

AddAchievement_2
Once it is done, add language as per below screenshot.
AddAchievement_3

Once you have created leaderboard and achievements, now move back to you application detail and scroll down to switch on Game Center . Once it is enabled, you need to add leaderboard and achievement which you created as explained earlier.  See below screenshot for more details:

Apple game center

Now our iTunes connect part is done and we will move to our second step of integration. I require you to follow these 5 steps of process in Xcode involving player authentication, score submission to leader boards, displaying of achievements and leader boards, managing / ranking the achievements, and resetting them.

Step 1 – Authenticate Player

Here is the source code require to authenticate player with Game Center.

Open ViewController.h file and insert below source code.

@interface ViewController ()
...
//this is declaration of Method
-(void)authenticateLocalPlayer;
@end

Write below code in ViewController.m file

-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
//this will open GameCenter Login Screen
[self presentViewController:viewController animated:YES completion:nil];
}
else{
if ([GKLocalPlayer localPlayer].authenticated) {
_gameCenterEnabled = YES;
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^( NSString * leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
else{
_leaderboardIdentifier = leaderboardIdentifier;
}
}];
}
else{
_gameCenterEnabled = NO;
}
}
};
}
-(void)ViewDidLoad{
[super: ViewDidLoad];
[self authenticateLocalPlayer];
}

Step 2: Source Code of Score Reporting

Write below code in ViewController.h file

@interface ViewController ()
...
-(void)reportScore;
@end

Write below code in ViewController.m file

-(void)reportScore{
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
score.value = _score;
[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
- (IBAction)handleAnswer:(id)sender {
....
if (_lives == 0) {
....
[self reportScore];
}
else
{
if (your answer is wrong)
{
_lives--;
}
else
{
move on;
}
}
....
}
-(void)updateTimerLabel:(NSTimer *)timer{
....
if (_timerValue > 60) {
....
[self reportScore];
}
}

Step 3: Source Code for Displaying Leaderboard And Achievements

Write below code in ViewController.h file

@interface ViewController : UIViewController
...
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard;
@end

Write below code in ViewController.m file

-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{
/*here “_leaderboardIdentifier” is your Unique Leaderboard ID which was saved earlier see above image*/
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
gcViewController.gameCenterDelegate = self;
if (shouldShowLeaderboard) {
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
gcViewController.leaderboardIdentifier = _leaderboardIdentifier;
}
else{
gcViewController.viewState = GKGameCenterViewControllerStateAchievements;
}
[self presentViewController:gcViewController animated:YES completion:nil];
}
- (IBAction)showGameCenterOptions:(id)sender {
....
[_customActionSheet showInView:self.view
withCompletionHandler:^(NSString *buttonTitle, NSInteger buttonIndex) {
if ([buttonTitle isEqualToString:@"View Leaderboard"]) {
[self showLeaderboardAndAchievements:YES];
}
else if ([buttonTitle isEqualToString:@"View Achievements"]) {
[self showLeaderboardAndAchievements:NO];
}
else{
}
}];
....
}
//GameCenterViewController Delegate Method
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
}

Step 4: Source Code to Manage Achievements

Write below code in ViewController.h file

@interface ViewController ()
...
-(void)updateAchievements;
@end

Write below code in ViewController.m file

-(void)updateAchievements{
NSString *achievementIdentifier;
float progressPercentage = 0.0;
BOOL progressInLevelAchievement = NO;
GKAchievement *levelAchievement = nil;
GKAchievement *scoreAchievement = nil;
if (_currentAdditionCounter == 0) {
if (_level <= 3) {
progressPercentage = _level * 100 / 3;
achievementIdentifier = @"Achievement_Level3";
progressInLevelAchievement = YES;
}
else if (_level < 6){
progressPercentage = _level * 100 / 5;
achievementIdentifier = @"Achievement_Level5Complete";
progressInLevelAchievement = YES;
}
}
if (progressInLevelAchievement) {
levelAchievement = [[GKAchievement alloc] initWithIdentifier:achievementIdentifier];
levelAchievement.percentComplete = progressPercentage;
}
if (_score <= 50) {
progressPercentage = _score * 100 / 50;
achievementIdentifier = @"Achievement_50Points";
}
else if (_score <= 120){
progressPercentage = _score * 100 / 120;
achievementIdentifier = @"Achievement_120Points";
}
else{
progressPercentage = _score * 100 / 180;
achievementIdentifier = @"Achievement_180Points";
}
scoreAchievement = [[GKAchievement alloc] initWithIdentifier:achievementIdentifier];
scoreAchievement.percentComplete = progressPercentage;
NSArray *achievements = (progressInLevelAchievement) ? @[levelAchievement, scoreAchievement] : @[scoreAchievement];
[GKAchievement reportAchievements:achievements withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
- (IBAction)handleAnswer:(id)sender {
...
if (shouldContinue){
...
[self updateAchievements];
}
}

Step 5: Source Code to Reset Achievements

Write below code in ViewController.h file
@interface ViewController ()
...
-(void)resetAchievements;
@end

Write below code in ViewController.m file
-(void)resetAchievements{
[GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}

Now run your app in iOS device or simulator and test your app.

If you are iOS developer and still finding any issue, write your query by comment. I will share best possible solution to your query.

If you are business owner and looking for iOS application development services, contact us now!

About the author

1 Response

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.