Search code examples
iphonecocoa-touchgamekitgame-centerobjective-c-blocks

Game center authentication block keeps getting called


So, I noticed that after calling initializeGameCenter() once, every time my application gets back to the foreground the below block(after authenticateWithCompletionHandler) is getting called - is this regular behavior of Game Center ?? (I made sure to place a breakpoint to verify that only the block is getting called but not the initializeGameCenter itself)

- (void)initializeGameCenter
{
    // Don't initialize Game Center unless we have access to the classes from iOS 4.1 or greater. 

    if (![self isGameCenterAvailable]) {
        return;
    }



    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {

        NSDictionary *userInfo = nil;
        if (error == nil) {
            // Game Center will present a "Welcome Back" message when we have authenticated
            GTMLoggerInfo(@"Game Center successfully authenticated");
        }
        else {
            userInfo = [NSDictionary dictionaryWithObject:error forKey:@"NSError"];
            GTMLoggerDebug(@"error authenticating game center");
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:GameCenterAuthenticateDidFinishNotification
                                                            object:self
                                                          userInfo:userInfo]; 

    }];
}

Solution

  • From the Game Kit Programming Guide:

    " [when] your game moves back to the foreground, Game Kit authenticates the player, and your authentication handler is called."

    http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html#//apple_ref/doc/uid/TP40008304-CH8-SW11

    I.e. your completion block will get called every time your app moves to the foreground, as if you had called -[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:]