Can I manually invite a user to log in to Game Center on iOS 7?

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?

+6
source share
1 answer

You can first check if the player’s authentication has passed or not by reading the GKLocalPlayer object.

If you do not have an authenticated user, you can open the game center application. The disadvantage of this method is that after the user authenticates through the game center application, he is still in the game center application and must "switch back" to your application. When it switches back, the authentication handler that you defined in the code is activated.

 -(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: Please note that in the Apple documentation they say that you should not ask the user to log in or show a login prompt. The automatic way (which already has your code) should be accepted. Displaying the warnings described above simply helps the user enter the game center, since you should not force the application to display a dialog.

+6
source

Source: https://habr.com/ru/post/957400/


All Articles