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