Ios-gamekit

提供:Dev Guides
移動先:案内検索

iOS-GameKit

Gamekitは、iOSアプリケーションにリーダーボード、実績、およびその他の機能を提供するフレームワークです。 このチュートリアルでは、リーダーボードの追加とスコアの更新に必要な手順を説明します。

関与するステップ

  • ステップ1 *-iTunes接続で、*一意のアプリID *があり、*バンドルID *を使用してアプリケーションの更新を作成し、対応するプロビジョニングプロファイルを使用してXcodeでコード署名することを確認します。
  • ステップ2 *-新しいアプリケーションを作成し、アプリケーション情報を更新します。 これについては、apple-addの新しいアプリのドキュメントで詳しく知ることができます。

ステップ3 *-アプリケーションのページの *Manage Game Center でリーダーボードを設定し、単一のリーダーボードを追加して、*リーダーボードID *およびスコアタイプを指定します。 ここでは、finddevguidesとしてリーダーボードIDを提供します。

  • ステップ4 *-次のステップは、コードの処理とアプリケーションのUIの作成に関連しています。

ステップ5 *-*シングルビューアプリケーション*を作成し、*バンドルID *を入力します。これは、 iTunes接続*で指定されたIDです。

  • ステップ6 *-以下に示すようにViewController.xibを更新します-

iOSチュートリアル

ステップ7 *-プロジェクトファイルを選択し、 *targets を選択してから GameKit.framework を追加します。

ステップ8 *-追加したボタンの *IBActions を作成します。

ステップ9 *- *ViewController.h ファイルを次のように更新します-

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController
<GKLeaderboardViewControllerDelegate>

-(IBAction)updateScore:(id)sender;
-(IBAction)showLeaderBoard:(id)sender;

@end

ステップ10 *- *ViewController.m を次のように更新します-

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   if([GKLocalPlayer localPlayer].authenticated == NO) {
      [[GKLocalPlayer localPlayer]
      authenticateWithCompletionHandler:^(NSError *error) {
         NSLog(@"Error%@",error);
      }];
   }
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
  //Dispose of any resources that can be recreated.
}

- (void) updateScore: (int64_t) score
   forLeaderboardID: (NSString*) category {
   GKScore *scoreObj = [[GKScore alloc]
   initWithCategory:category];
   scoreObj.value = score;
   scoreObj.context = 0;

   [scoreObj reportScoreWithCompletionHandler:^(NSError *error) {
     //Completion code can be added here
      UIAlertView *alert = [[UIAlertView alloc]
      initWithTitle:nil message:@"Score Updated Succesfully"
      delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
      [alert show];
   }];
}

-(IBAction)updateScore:(id)sender {
   [self updateScore:200 forLeaderboardID:@"finddevguides"];
}

-(IBAction)showLeaderBoard:(id)sender {
   GKLeaderboardViewController *leaderboardViewController =
   [[GKLeaderboardViewController alloc] init];
   leaderboardViewController.leaderboardDelegate = self;
   [self presentModalViewController:
   leaderboardViewController animated:YES];
}

#pragma mark - Gamekit delegates
- (void)leaderboardViewControllerDidFinish:
(GKLeaderboardViewController *)viewController {
   [self dismissModalViewControllerAnimated:YES];
}
@end

出力

アプリケーションを実行すると、次の出力が得られます-

iOSチュートリアル

「リーダーボードを表示」をクリックすると、次のような画面が表示されます-

iOSチュートリアル

「スコアの更新」をクリックすると、スコアがリーダーボードに更新され、以下に示すようなアラートが表示されます-

iOSチュートリアル