Game Center in iOS Development

Game Center of iOS Development is used for game ranking and other vainity expression. Before develop on it, the following steps are needed.

Register a App ID in developer member center, and check the Game Center option.
Register the App in iTunesConnect, and enable the Game Center.
Add leaderboards and achievements according to the instructions on page. The parameters defined here would be used in code.
After that, you can put the Game Center related codes into projects now.

Anyway, you should have a Tester Account (Same with Apple ID but only login for Sandbox Game Center) for testing, and open the SandBox option in Settings - Game Center before you start to debug your app.

## Game Center Entrance

### Loader ViewController

The ViewController needs import GameKit Header, and implement GKGameCenterControllerDelegate Protocol.

```Objective-C
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface StartViewController : UIViewController
<GKGameCenterControllerDelegate>

@end
```

In its preparation process, call the authenticate function. This would present a Game Center Login view controller.

```Objective-C
- (void)authenticateLocalPlayer{
    GKLocalPlayer* player = [GKLocalPlayer localPlayer];
    player.authenticateHandler = ^(UIViewController* ui, NSError* error ){
        if( nil != ui ){
            [self presentViewController:ui animated:YES completion:nil];
        }
    };
}
```

When the opened Game Center view controller shown and closed, the protocol defined function gameCenterViewControllerDidFinish: would be called.

```Objective-C
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController{
    [self dismissViewControllerAnimated:YES completion:nil];
}
```

By the way, if you want to put a button to enter the leaderboard to view ranking and achievements, use the following sample codes to create and open a GKGameCenterViewController.

```Objective-C
-(void)onGameCenterBtn:(id)sender{    
    GKGameCenterViewController *gcView = [GKGameCenterViewController new];
    if (gcView != nil){
        gcView.gameCenterDelegate = self;
        gcView.viewState = GKGameCenterViewControllerStateLeaderboards;
        [self presentViewController:gcView animated:YES completion:nil];
    }
}
```

## Usage Tool

There are commonly two methods to deal with Game Center, one is leaderboard, the other is achievement. For simple, I made them into one tool class.

Just use this GameCenterHelper class to deal with Game Center upload job!

### GameCenterHelper.h

```Objective-C
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface GameCenterHelper : NSObject

/**
 Send value to Game Center Leaderboard for LBID, and do completionHandler(error) after done.
 */
+(void)sendValue:(NSNumber*)value toLeaderboardWithID:(NSString*)LBID complete:(void(^)(NSError *error))completionHandler;

/**
 Report achievement complete percent to Game Center for identifier, and do completionHandler(error) after done.
 */
+(void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent withCompletionHandler:(void(^)(NSError *error))completionHandler;

@end
```

### GameCenterHelper.m

```Objective-C
#import "GameCenterHelper.h"

@implementation GameCenterHelper

+(void)sendValue:(NSNumber*)value toLeaderboardWithID:(NSString*)LBID complete:(void(^)(NSError *error))completionHandler{
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        GKScore* score = [[GKScore alloc] initWithLeaderboardIdentifier:LBID];
        score.value = [value integerValue];
        [GKScore reportScores:@[score] withCompletionHandler:completionHandler];
    }else{
        //Log(@"GC sendValue not isAuthenticated");
    }
}

+(void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent withCompletionHandler:(void(^)(NSError *error))completionHandler{

    GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
    if(achievement){
        achievement.percentComplete=percent;
        [GKAchievement reportAchievements:@[achievement] withCompletionHandler:completionHandler];
    }
}

@end
```

----
[自分のブログから移転してきたものである。](http://www.everstray.com/news/news.php?newsCode=MjAxNTAyMjQxNjQwMjNbR2FtZSBDZW50ZXIgaW4gaU9TIERldmVsb3BtZW50XUV2ZXJzdHJheS5sb2c%3D)