Frage

According to Apple's Game Center programming guide, this code sets up an authentication handler. If you run this at the beginning of your game, the first time you run it, it will prompt the user to log in if they haven't yet.

- (void)authenticateLocalPlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            NSLog(@"Player not authenticated.");
        } else if (localPlayer.isAuthenticated) {
            NSLog(@"Authentication successful.");
        } else {
            NSLog(@"Authentication failed. Error: %@.",error);
        }
    };
}

Suppose that the user hasn't logged in yet, and cancels the authentication screen to play the game normally.

There is a button for playing a multiplayer match in my game. If the user presses the button, it will attempt to search for other players by presenting a GKMatchmakerViewController instance.

Since the player isn't logged in, the player will actually receive an error dialog saying that they aren't logged in. The dialog only has an OK button, which dismisses it.

If the player insists on pressing this button, the same dialog will come.

However, this is an odd behaviour. It would be more reasonable that if the player wants to play a multiplayer match but isn't logged in yet, the game will prompt the user to log in.

The above code sets up a handler, so it really isn't what I'm looking for. However, I made a breakpoint and noticed that viewController is a GKHostedAuthenticateViewController instance. I figured that maybe I could create an instance of that class and present it, which should technically be equivalent to prompting the user to log in.

However, Xcode doesn't seem to recognise that class when I write it. I'm under the impression that I'm not allowed to do this.

How can I manually prompt the user to log in to Game Center?

War es hilfreich?

Lösung

You can first check if the player is authenticated or not, by reading the GKLocalPlayer object.

If there is not authenticated user, you can open the game centre app. The downside with this method is that after the user authenticates through the game centre app, he is still in the game centre app and has to "switch back" to your app. When he switches back, the authentication handler you defined in your code gets triggered.

-(void)clickedOnStartGame
{
    if (_signedIn)
    {
        //Do what you need to.
    }
    else if (!_signedIn)
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Game Center"
                              message:@"If Game Center is disabled try logging in through the Game Center app"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:@"Open Game Center", nil];
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"gamecenter:"]];
    }
}

EDIT: Note, that in Apple's documentation, they say that you shouldn't be prompting the user to log in again, or show a login prompt. The automated way, (which your code already has) is supposed to be the accepted way. Showing the alert view I described above just helps the user log into game centre since you aren't supposed to force the app to show a dialogue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top