Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GKLocalPlayer authentication not working, but isAuthenticated returns YES (Game Center sandbox)

I'm working on integrating turn based matches in my game and a few days ago I started getting weird errors from the GameKit API saying that the local player is not authenticated, even though he is.

When I launch the app, the authenticateHandler is called, the view controller is displayed and after entering the password, the authenticaHandler is called again and the local player seems to be authenticated. isAuthenticated returns YES.

But as soon as I start using any of the GameKit APIs like loadFriendsWithCompletionHandler:, an error is returned saying that the player has not been authenticated.

This is the code for handling authentication changes.

[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(UIViewController *viewController, NSError *error) {
    if ([[GKLocalPlayer localPlayer] isAuthenticated]) {
        // Player authenticated
    } else {
        // Player not authenticated

        if (viewController != nil) {
            // Present view controller
        }
    }
}];

And this is the error message I receive when calling any of the GameKit methods. Please note that -isAuthenticated still returns YES when the error is returned.

Error finding match: Error Domain=GKErrorDomain Code=6 "The requested operation could not be completed because local player has not been authenticated." UserInfo=0x14e9f950 {NSLocalizedDescription=The requested operation could not be completed because local player has not been authenticated.}
(lldb) print (BOOL) [[GKLocalPlayer localPlayer] isAuthenticated] (BOOL) $3 = YES

I'm testing in the Game Center sandbox and it started happening a few days ago. Previously, I didn't experience the problem at all.
It only happens about one of three times when the app is started. I have tried deleting the app, restarting my devices, cleaning the build folder and everything else I could think of.

Am I missing something or has anybody else experienced similar problems?

like image 412
Fabian Kreiser Avatar asked May 08 '14 12:05

Fabian Kreiser


1 Answers

This rich Apple documentation is a great place to look at. Here are 2 things which I would suggest -

  1. Game Center fails to complete authentication if your device has incorrect dates. So, go ahead and check the current date.

  2. You might have done this. I trust you - iOS Simulator >> Reset Content and Settings

Do you think there could be something wrong with the way you are using -[GKLocalPlayer loadFriendsWithCompletionHandler:]? Nothing wrong with your authentication function above, but I'd love to share mine if it works for you -

-(void)authenticateLocalPlayer{

    // Instantiate a GKLocalPlayer object to use for authenticating a player.
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {

            // If it's needed display the login view controller.
            [self presentViewController:viewController animated:YES completion:nil];

        }
        else {
            if ([GKLocalPlayer localPlayer].authenticated) {
                // If the player is already authenticated then indicate that the Game Center features can be used.

                _gameCenterEnabled = YES;

            }

            else {
                _gameCenterEnabled = NO;
            }
        }
    };
}
like image 167
raurora Avatar answered Sep 22 '22 14:09

raurora