IOS GameKit
簡介
GameKit是iOS SDK中一個常用(yòng)的框架。其核心功能(néng)有3個:
交互遊戲平台Game Center,
P2P設備通訊功能(néng)
In-Game Voice。
實例步驟
1.在鏈接 iTunes 時(shí)請(qǐng)确保擁有一個唯一的 App ID( unique App ID),App ID在我們應用(yòng)程序更新 bundle ID時(shí)及在Xcode代碼簽名與相應的配置文(wén)件需要使用(yòng)到(dào)。
2.創建新的應用(yòng)程序和(hé)更新應用(yòng)程序信息。在添加新的應用(yòng)程序文(wén)檔可以了(le)解更多有關信息。
3.打開(kāi)你(nǐ)申請(qǐng)的application,點擊Manage Game Center選項。進入後點擊Enable Game Center使你(nǐ)的Game Center生效。接下(xià)來(lái)設置自(zì)己的Leaderboard和(hé)Achievements。
4.下(xià)一步涉及處理(lǐ)代碼,并爲我們的應用(yòng)程序創建用(yòng)戶界面。
5.創建一個single view application,并輸入 bundle identifier 。
6.更新 ViewController.xib。
7.選擇項目文(wén)件,然後選擇目标,然後添加GameKit.framework
8.爲已添加的按鈕創建IBActions
9.更新ViewController.h文(wén)件,如下(xià)所示
#import
#import
@interface ViewController : UIViewController
-(IBAction)updateScore:(id)sender;
-(IBAction)showLeaderBoard:(id)sender;
@end
10.更新ViewController.m ,如下(xià)所示
#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
網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發