Вопрос

Basically under ViewController's viewdidload method, I put:

[self authenticateLocalUser];

And authenticateLocalUser method is:

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

if (localPlayer.isAuthenticated == NO)
{
    NSLog(@"FAILED");
}

Why doesn't it authenticate? Shouldn't the class method for GKLocalPlayer authenticate automatically? Also, if it does authenticate, should I get the "welcome back __" on the top? I am not getting this banner at all. Is there something I need to do beforehand?

Это было полезно?

Решение

In iOS6 and above you need to present Game Center login view with setAuthenticateHandler api.

Here is Code:

#define CHECK_IOS_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] \
compare:version options:NSNumericSearch] == NSOrderedAscending)

- (void) authenticateLocalUser
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    if (CHECK_IOS_LESS_THAN(@"6.0"))
    {
        // ios 5.x and below
        [localPlayer authenticateWithCompletionHandler:^(NSError *error)
         {
             [self checkLocalPlayer];
         }];
    }
    else
    {        
        // ios 6.0 and above
        [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error)
                                             {
                                                 if (!error && viewcontroller)
                                                 {
                                                     [self.navController
                                                      presentViewController:viewcontroller animated:YES completion:nil];
                                                 }
                                                 else
                                                 {
                                                     [self checkLocalPlayer];
                                                 }
                                             })];
    }
}



- (void)checkLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    if (localPlayer.isAuthenticated)
    {
        /* Perform additional tasks for the authenticated player here */

        [self  processGameCenterLoginSuccess];
    }
    else
    {
        /* Perform additional tasks for the non-authenticated player here */
        [self processGameCenterLoginFaild];
    }
}

After adding this code, goto device settings and Reset: Device: Settings->General->Reset->Reset All Settings Simulator: Settings->General->Reset->Reset Locations and Privacy

Другие советы

You need to install an authenticateHandler. A section in Apple's Game Center Programming Guide gives a verbatim example. The code starts like this (see that section as linked for full source code):

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = // ...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top