Apple Game Centerサービスのリーダーボードを使ってみた
Game Centerのリーダーボードについて説明します。
Game Center
Game CenterはiOS4.x以降から利用できる、Apple社が提供するゲームSNSサービスです。Game CenterにApple IDでサインインすることでiOSのGame Center対応アプリと連携し、アプリ側からスコアを保存することができ、ランキング表示、オンライン対戦等の実装が可能になります。
Game Centerの主な機能
- リーダーボード
- アチーブメント
- チャレンジ
- マッチメーク
- リアルタイム対戦
- ホスト型の対戦
- ターン制の対戦
リーダーボードについて
ゲームスコアのランキング用レコードで、1アプリにつき、最大500個作成できる。レコードを複数のアプリで共有することもできる。
保存できるデータ
スコアデータとして64bit整数のみ保存可能、また、スコアデータの他にスコアコンテキストというものがあり、同じく64bit整数が保存できる。
ログイン認証
Sample code
// Authenticate
- (void)authenticateLocalPlayer
{
GKLocalPlayer __weak *localPlayer;
localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController* viewController, NSError* error){
if (nil != viewController) {
// Show authentication dialog controller
if ([self.delegate respondsToSelector:@selector(gameCenterManagerRequestedShowAuthenticationDialog:)]) {
// Notify it
[self.delegate gameCenterManagerRequestedShowAuthenticationDialog:viewController];
}
}
else if (localPlayer.authenticated) {
// Load friends
[localPlayer loadFriendsWithCompletionHandler:^(NSArray* friendIDs, NSError* error) {
// Notify
if ([self.delegate respondsToSelector:@selector(gameCenterManagerAuthenticateSuccess:)]) {
// Notify it
[self.delegate gameCenterManagerAuthenticateSuccess:localPlayer];
}
}];
}
else {
if ([self.delegate respondsToSelector:@selector(gameCenterManagerAuthenticateFaild)]) {
// Notify it
[self.delegate gameCenterManagerAuthenticateFaild];
}
}
};
}
CKLocalPlayerを作成し、authenticateHandlerプロパティにBlockを設定する。Block引数の「viewController」が認証用コントローラーとなっている。
↓認証用コントローラーのキャプチャ
ランキングの表示方法
・GKGameCenterViewControllerを使うのが簡単。GameCenterを利用するための一通りのUIを提供してくれる。
Sample code
- (void)_showgameCenterController
{
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (nil != gameCenterController)
{
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
[self presentViewController: gameCenterController animated: YES completion:nil];
}
}
↓GKGameCenterViewControllerのキャプチャ
ランキング表示を独自実装するために用いるGame Kitのクラス
ランキング表示を独自で実装する場合は、下記のクラスを使います。
クラス名 | クラスの機能 |
---|---|
GKScore | ゲーム本体の中でGKScoreオブジェクトを生成し、こ れを使ってGame Center上のLeaderboardにスコアを送信します。逆に Leaderboardからスコア情報を取得する場合も、スコアはGKScoreオブ ジェクトの形で得られます。 |
GKLeaderboard | Leaderboardからスコア データを検索する際にGKLeaderboardオブジェクトを生成しま す。 |
GKLocalPlayer | GameCenterにサインインしているユーザーの詳細情報を管理します |
Sample Code
スコアの送信
// Create score
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier:identifier];
// Set score value
scoreReporter.value = score;
// Set context value
scoreReporter.context = 0;
// Report score
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {}];
ランキングの取得
// Create leaderboard
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
// Set player scope (GKLeaderboardPlayerScopeGlobal or GKLeaderboardPlayerScopeFriendsOnly)
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
// Specifying the duration
leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday;
// Decide record from which to get
leaderboardRequest.identifier = @"Combined.LandMaps"
// Specifies the top 10
leaderboardRequest.range = NSMakeRange(1,10);
// Request
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
// スコア情報を処理する。
}];
所感
サーバを用意しなくても、アプリにランキング機能をつけられるので便利です。欲を言うと、単純な整数だけでなく、文字列も送れるようになってほしい。
参考情報
https://developer.apple.com/jp/devcenter/ios/library/documentation/GameKit_Guide.pdf