According to the Apple Game Center Programming Guide, this code installs an authentication handler. If you run this at the beginning of your game, the first time it starts , it will prompt the user to log in if they have not already.
- (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); } }; }
Assume that the user has not logged in yet and cancels the authentication screen in order to play the game normally.
My game has a button for playing multiplayer. If the user clicks the button, he will try to find other players by presenting an instance of GKMatchmakerViewController .
Since the player has not logged in, the player will actually receive an error message stating that they are not logged in. There is an OK button in the dialog box that rejects it.
If the player insists on pressing this button, the same dialog will appear.
However, this is a strange behavior. It would be more reasonable if the player wants to play in a multiplayer match, but has not yet logged in, the game will prompt the user to log in.
The above code sets up a handler, so this is really not what I'm looking for. However, I made a breakpoint and noticed that viewController is an instance of GKHostedAuthenticateViewController . I thought that maybe I could create an instance of this class and present it, which should be technically equivalent to asking the user to log in.
However, Xcode does not seem to recognize this class when I write it. I get the impression that I'm not allowed to do this.
How can I manually request a user to log in to Game Center?