I have successfully added Game Center capabilities to my app. When the app is opened it successfully authenticates the user and shows the "Welcome back (UserName)" banner.
However, I am not sure of how to add a leaderboard to the game. I was wondering if someone could help me A: Help me understand how to link the leaderboard i made in iTunes connect with the app and make highscore the value of the leaderboard. And B: Make the leaderboard show up in the app with all the rankings.
All the code for gamecenter in my app so far is below.
Interface File:
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@interface GCHelper : NSObject {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}
@property (assign, readonly) BOOL gameCenterAvailable;
+ (GCHelper *)sharedInstance;
-(void)authenticateLocalUser;
@end
Implementation File:
#import "GCHelper.h"
@implementation GCHelper
@synthesize gameCenterAvailable;
#pragma mark initialization
static GCHelper *sharedHelper = nil;
+ (GCHelper *) sharedInstance {
if (!sharedHelper) {
sharedHelper = [[GCHelper alloc] init];
}
return sharedHelper;
}
- (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);
}
- (id) init {
if ((self = [super init])) {
gameCenterAvailable = [self isGameCenterAvailable];
if(gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
-(void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
NSLog(@"Authentication changed: player not authenticated");
userAuthenticated = FALSE;
}
}
#pragma mark User Functions
-(void) authenticateLocalUser {
if(!gameCenterAvailable) return;
NSLog(@"Authentication local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
} else {
NSLog(@"Already authenticated!");
}
}
@end
OKAY, so. The code is working now but when the code to access the leaderboard returns an error, I have no code to handle it and instead just makes the app go into a frozen state and makes it unable to function.
Code being called to access leaderboard :
- (void) presentLeaderboards
{
GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
gameCenterController.gameCenterDelegate = self;
[self presentViewController:gameCenterController animated:YES completion:nil];
}