Xcode6 GameCenter 実装
2016/06/03
Xcode6(iOS7以上)でのGameCenter実装
検索であまり見つからなかったので、シェア( ゚∀゚)・;”.、
▽まずはGameCenterにログイン(アプリ内)
AppDelegate.h
#import <GameKit/GameKit.h>をインポート
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //gamecenterのプレイヤーを取得 GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){}; return YES; }
▽スコアを送信する前に下ごしらえ
ViewController.h
#import <GameKit/GameKit.h> @interface ViewController : UIViewController <GKGameCenterControllerDelegate> { }
ViewController.m
// ストーリボード表示 - (void)GamecenterInit { GKGameCenterViewController *leaderboardController = [GKGameCenterViewController new]; if (nil != leaderboardController) { leaderboardController.gameCenterDelegate = self; leaderboardController.viewState = GKGameCenterViewControllerStateLeaderboards; [self presentViewController:leaderboardController animated:YES completion:nil]; } } // ストーリボードを閉じる際に必要なメソッド // GKLeaderboardViewControllerのDelegate -(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController { [self dismissViewControllerAnimated:YES completion:nil]; } // GKAchievementViewControllerのDelegate -(void)achievementViewControllerDidFinish:(GKGameCenterViewController *)viewController { [self dismissViewControllerAnimated:YES completion:nil]; } // GameCenter -(void)submitScore:(int)score forCategory:(NSString*)category { GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier:category]; scoreReporter.value = score; [GKScore reportScores:@[scoreReporter] withCompletionHandler:^(NSError *error) { if (error) { /* エラー処理 */ NSLog(@"書き込めませんでした"); } }]; }
▽ストーリーボード表示
表示する為のボタン(メソッド)に以下をコピペ
[self GamecenterInit];
▽スコアを送る方法
お好きなタイミングで(ゲーム終了とかで)下の行を添えるだけ!
// ゲームセンターへ通知 [self submitScore:755 forCategory:@"iTunesConnectで付けたID"];
注意:例として代入してある 755 の箇所は整数のみ
おやくにたてたら幸いです.。゚+.(・∀・)゚+.゚
良いねとかコメントもらえると励みになります☆
関連記事
-
-
BGM・SE 導入方法
音楽:導入方法 1… AVFoundationフレームワークを追加 2… hファ …
-
-
乱数のこと
乱数(ランダムな数)を取り出す際 rand や arc4random を使用しま …
-
-
iPhone画面サイズ
3.5inch 320 x 480px (Retina 640 x 960px) …
-
-
UIImageView 画像の差し替え
[UIImageView alloc] 後の画像を変換する時は下記コードを参照し …
-
-
UILabelをコードで生成(Tips付)
Main Storyboard (IB)に用意されている Label を使わずに …
-
-
アプリの名前をつける
iPhoneのホーム画面に表示される「アプリ名」の付け方 アプリ名の変更には上 …
-
-
CheatSheet
Xcodeの便利なショートカットキー 良く使ってるなと感じたら追記 Cmd+Al …
-
-
タッチイベント:有効・無効
// タッチイベントを有効にしたい場合の処理 [[UIApplication s …
-
-
UIView
タイトル画面の背景など、新たにビューを追加した場合 UIViewを生成し sel …
-
-
UIViewの背景画像をフィットさせたい
UIViewは(UIImageViewとは違い)背景画像のサイズをフィットする機 …
- PREV
- PostScriptの調べ方
- NEXT
- UIBezierPath
Comment
すみません。
ゲームセンターへ通知のiTunesConnectで付けたIDとは何を指すのでしょうか?
どうも初めまして
iTunesConnectでアプリの情報を登録ををしますよね?
Gamecenterで使う為のリーダーボードを作る時に「LeaderboardID」というのを登録します。
そのIDのことです。