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?