Gamekit是一个框架,可为iOS应用程序提供排行榜,成就和更多功能。在本教程中,无涯教程将说明添加排行榜和更新分数的步骤。
步骤1 - 在iTunes connect中,确保您具有唯一的应用ID ,并且当无涯教程使用捆绑程序ID 和代码签名创建应用程序更新时在Xcode中具有相应的配置文件。
步骤2 - 创建一个新的应用程序并更新应用程序信息。您可以在apple-add新应用程序文档中了解有关此内容的更多信息。
步骤3 - 在应用程序页面的管理游戏中心中设置排行榜,在其中添加单个排行榜并提供排行榜ID 和得分类型。在这里,无涯教程将排行榜ID作为LearnfkPoint。
步骤4 - 下一步与处理代码和为无涯教程的应用程序创建UI有关。
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-gamekit.html
来源:LearnFk无涯教程网
步骤5 - 创建一个单视图应用程序,并输入捆绑包标识符是 iTunes connect 中指定的标识符。
步骤6 - 如下所示更新ViewController.xib-
步骤7 - 选择项目文件,然后选择目标,然后添加 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:@"LearnfkPoint"]; } -(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
运行应用程序时,将获得以下输出-
当无涯教程点击"Show Leaderboard"时,无涯教程将得到类似于以下内容的屏幕:
当无涯教程单击"Update score"时,分数将更新到无涯教程的排行榜,并且无涯教程将收到如下所示的警报-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)