鍍金池/ 教程/ iOS/ iOS - GameKit
iOS - Switches(切換/開關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購買
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門
iOS - SQLite 數(shù)據(jù)庫
iOS - GameKit
IOS - 訪問地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

iOS - GameKit

簡介

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,如下所示。

iOS Tutorial

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ì)得到下面的輸出。

iOS Tutorial

當(dāng)我們單擊“顯示領(lǐng)先者板上,我們會(huì)得到一個(gè)類似于下面的屏幕。

iOS Tutorial

當(dāng)我們點(diǎn)擊更新分?jǐn)?shù),比分將被更新到我們領(lǐng)先板上,我們會(huì)得到一個(gè)警告,如下圖所示。

iOS Tutorial