涉及步骤
步骤 1 − 在 iTunes 连接中,确保您有一个 unique App ID 当我们创建应用程序更新时 bundle ID 并使用相应的配置文件在 Xcode 中进行代码签名。
步骤 2− 创建新应用程序并更新应用程序信息。您可以在 apple-add new apps 文档中了解更多相关信息。
步骤 3 − 设置排行榜 Manage Game Center 在您的应用程序页面中添加一个排行榜并给出 leaderboard ID和分数类型。这里我们将排行榜 ID 作为 tutorialsPoint。
步骤 4 - 接下来的步骤与处理代码和为我们的应用程序创建 UI 相关。
步骤 5 − 创建一个 single view application 并输入 bundle identifier 是指定的标识符 iTunes connect.
步骤 6 - 更新 ViewController.xib 如下所示 -
步骤 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:@"tutorialsPoint"];
}
-(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