I've followed a few tutorials & Apple documentation on authenticating the local player for Game Center functionality using Objective C. Currently, our beta version has a functioning Game Center authentication for leaderboards. However, it seems that something is wrong with the authentication for achievements, because none of our achievements are coming across from iTunes Connect. From ViewController.m, the first authentication occurs as shown below:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(showAuthenticationViewController)
name:PresentAuthenticationViewController
object:nil];
[[GameKitHelper sharedGameKitHelper] authenticateLocalPlayer];
}
- (void)showAuthenticationViewController
{
GameKitHelper *gameKitHelper = [GameKitHelper sharedGameKitHelper];
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: gameKitHelper.authenticationViewController animated:YES completion:nil]; }
Within GameKitHelper, the next authentication step occurs:
- (void)authenticateLocalPlayer
{
//1
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
//3
[self setLastError:error];
if(viewController != nil) {
//4
[self setAuthenticationViewController:viewController];
} else if([GKLocalPlayer localPlayer].isAuthenticated) {
//5
_enableGameCenter = YES;
NSLog(@"Successful");
} else {
//6
_enableGameCenter = NO;
NSLog(@"Failure");
}
};
}
When loading achievements, the following function is used:
-(void)achievementLoad {
if (_enableGameCenter) {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if (error != nil) {
NSLog(@"Error in loading achievements.");
}
if (achievements != nil) {
//Process achievements.
for (GKAchievement *temp in achievements) {
//temp
[[GameKitHelper sharedGameKitHelper].achievementsDictionary setObject:temp forKey:temp.identifier];
//[_achievementsDictionary setObject:temp forKey:temp.identifier];
}
NSLog(@"Achievements Retrievemented");
}
}];
} else {
NSLog(@"Achievements not loaded");
}
}
When attempting to retrieve achievements, the game will always report "Error Code 6: Player not authenticated" (error != nil is true). Then, when I try to utilize the achievements dictionary, it's set as nil, which doesn't make sense.
I've got iTunes Connect info active for Game Center, and I've logged in with a sandbox account. Is there anything else that I'm doing wrong?
Any help would be much appreciated. Thanks!