Game Center是一项游戏中提供社交游戏功能的apple网络服务。要支持多项gAME Center功能,需要向pple提供有关的app信息,因此需要创建iTunes ConnecT纪录,并向其中添加Game Center配置详细信息。
Apple为大家接入GameCenter提供了GameKit.framework,在需要使用GameCenter的类中都要导入GameKit.h;
在.h文件中加入协议“GKGameCenterControllerDelegate”.
1.游戏中心管理器
创建共享的游戏管理器,不仅可以将GameCenter功能放在独立的类中,还可以轻松的在新项目中添加GameCenter功能。
判断是否支持GameCenter:
//是否支持GameCenter
- (BOOL) isGameCenterAvailable
{
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
NSString *reqSysVer = @"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
2,身份验证
GameCenter是一种需要验证身份的服务,如果没有登录就得先验证身份,否则什么也做不了。
//身份验证
- (void)authenticateLocalUser{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
[self presentViewController:viewController animated:YES completion:nil];
}
else{
if ([GKLocalPlayer localPlayer].authenticated) {
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
else{
}
}];
}
else{
}
}
};
}
3.用户变更检测
//用户变更检测
- (void)registerFoeAuthenticationNotification{
NSNotificationCenter *nc = [NSNotificationCenterdefaultCenter];
[nc addObserver:self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
}
- (void)authenticationChanged{
if([GKLocalPlayer localPlayer].isAuthenticated){
}else{
}
}
4.提交得分
向GameCenter验证身份后,便可提交得分了。
创建GKStore对象:
- (void) reportScore: (int64_t) score forCategory: (NSString*) category{
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if(error != nil){
NSData *saveSocreData = [NSKeyedArchiver archivedDataWithRootObject:scoreReporter];
//未能提交得分,需要保存下来后继续提交
[self storeScoreForLater:saveSocreData];
}else{
NSLog(@"提交成功");
}
}];
}
- (void)storeScoreForLater:(NSData *)scoreData{
NSMutableArray *savedScoresArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"savedScores"]];
[savedScoresArray addObject:scoreData];
[[NSUserDefaults standardUserDefaults] setObject:savedScoresArray forKey:@"savedScores"];
}
若得分提交不成功,需要再重新提交得分
//重新提交分数
- (void)submitAllSavedScores{
NSMutableArray *savedScoreArray = [[NSMutableArrayalloc] initWithArray:[[NSUserDefaultsstandardUserDefaults] objectForKey:@"savedScores"]];
[[NSUserDefaultsstandardUserDefaults] removeObjectForKey:@"savedScores"];
for(NSData *scoreDatain savedScoreArray){
GKScore *scoreReporter = [NSKeyedUnarchiver unarchiveObjectWithData:scoreData];
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if(error != nil){
NSData *saveSocreData = [NSKeyedArchiver archivedDataWithRootObject:scoreReporter];
//未能提交得分,需要保存下来后继续提交
[self storeScoreForLater:saveSocreData];
}else{
NSLog(@"提交成功");
}
}];
}
}
5.显示排行榜
创建GKLocalboardViewController来显示排行榜.
- (void)showGameCenter{
GKGameCenterViewController *gameView = [[GKGameCenterViewController alloc] init];
if(gameView != nil){
gameView.gameCenterDelegate = self;
[gameView setLeaderboardCategory:@"com.xxxx.test"];
[gameView setLeaderboardTimeScope:GKLeaderboardTimeScopeAllTime];
[self presentViewController:gameView animated:YES completion:^{
}];
}
}
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController{
[self dismissViewControllerAnimated:YES completion:nil];
}
6.得分成就
挑战让用户可以通过GameCenter向玩家发起得分或成就方面的挑战。挑战分为四种:“无效”,“待处理”,“已结束”,“已谢绝”。
- (void)reportAchievment:(NSString *)identifier withPercentageComplete:(double)percentComplete{
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier:identifier];
[achievement setPercentComplete:percentComplete];
[achievement reportAchievementWithCompletionHandler:^(NSError *error) {
if(error != nil){
NSLog(@"error:%@", [error localizedDescription]);
}else{
NSLog(@"提交成就成功");
}
}];
}
gameCenter的接入到这边就ok了。