GameKit是 iOS 應(yīng)用程序框架,提供了領(lǐng)先及更多功能。在本教程中,我們將解釋領(lǐng)先板上及更新分?jǐn)?shù)步驟。
涉及的步驟
1. 在iTunes連接,確保您擁有一個(gè)獨(dú)特的應(yīng)用程序ID和相應(yīng)的配置文件捆綁ID和代碼簽名,在Xcode當(dāng)我們創(chuàng)建應(yīng)用程序更新。
2. 創(chuàng)建一個(gè)新的應(yīng)用和更新應(yīng)用程序的信息。可以知道更多關(guān)于這個(gè)蘋果添加新的應(yīng)用程序文檔。
3. 設(shè)置一個(gè)領(lǐng)先板上,在游戲中心管理應(yīng)用程序的頁面中添加一個(gè)排行榜,排行榜ID和分?jǐn)?shù)類型。在這里,我們給領(lǐng)先板上編號(hào)為 yiibaiID。
4. 接下來的步驟是關(guān)系到我們的應(yīng)用程序處理代碼和創(chuàng)建UI。
5. 創(chuàng)建一個(gè)單一的視圖應(yīng)用程序并進(jìn)入包標(biāo)識(shí)符指定的標(biāo)識(shí)符在iTunes連接。
6. 更新ViewController.xib,如下所示。
7. 選擇項(xiàng)目文件,然后選擇目標(biāo),添加GameKit.framework
8. 創(chuàng)建IBActions 為我們已經(jīng)添加的按鈕
9. 更新 ViewController.h 文件內(nèi)容如下
#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:@"tutorialsYiibai"]; } -(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
現(xiàn)在,當(dāng)我們運(yùn)行程序時(shí),我們會(huì)得到下面的輸出。
當(dāng)我們單擊“顯示領(lǐng)先者板上,我們會(huì)得到一個(gè)類似于下面的屏幕。
當(dāng)我們點(diǎn)擊更新分?jǐn)?shù),比分將被更新到我們領(lǐng)先板上,我們會(huì)得到一個(gè)警告,如下圖所示。