新聞中心
iOS GameKit
簡(jiǎn)介
GameKit是iOS SDK中一個(gè)常用的框架。其核心功能有3個(gè):

- 交互游戲平臺(tái)Game Center,
- P2P設(shè)備通訊功能
- In-Game Voice。
實(shí)例步驟
1.在鏈接 iTunes 時(shí)請(qǐng)確保擁有一個(gè)唯一的 App ID( unique App ID),App ID在我們應(yīng)用程序更新 bundle ID時(shí)及在Xcode代碼簽名與相應(yīng)的配置文件需要使用到。
2.創(chuàng)建新的應(yīng)用程序和更新應(yīng)用程序信息。在添加新的應(yīng)用程序文檔可以了解更多有關(guān)信息。
3.打開(kāi)你申請(qǐng)的application,點(diǎn)擊Manage Game Center選項(xiàng)。進(jìn)入后點(diǎn)擊Enable Game Center使你的Game Center生效。接下來(lái)設(shè)置自己的Leaderboard和Achievements。
4.下一步涉及處理代碼,并為我們的應(yīng)用程序創(chuàng)建用戶界面。
5.創(chuàng)建一個(gè)single view application,并輸入 bundle identifier 。
6.更新 ViewController.xib,如下所示
7.選擇項(xiàng)目文件,然后選擇目標(biāo),然后添加GameKit.framework
8.為已添加的按鈕創(chuàng)建IBActions
9.更新ViewController.h文件,如下所示
#import#import @interface ViewController : UIViewController -(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
輸出
運(yùn)行該應(yīng)用程序,輸出結(jié)果如下
當(dāng)我們單擊顯示排行榜時(shí),屏幕顯示如下:
當(dāng)我們點(diǎn)擊更新分?jǐn)?shù),比分將被更新到我們排行榜上,我們會(huì)得到一個(gè)信息,如下圖所示
分享題目:創(chuàng)新互聯(lián)IOS教程:iOS GameKit
鏈接地址:http://www.dlmjj.cn/article/dhhijeh.html


咨詢
建站咨詢
