Handling Game Center authentication

14,088

Solution 1

There is a problem with your code: First and foremost, you should set the authentication handler as soon as your app loads. This means that regardless of whether the localPlayer is authenticated or not, you set the handler so that it is automatically called if the player is logged out and logged back in again. If your player switches from your app to the game center app, and logs out / in, then the handler in your app won't be called (if he was already authenticated when the app first started up). The point of setting the handler is so that every time there is an auth change (in / out), your app can do the right thing.

Secondly, you shouldn't be relying on the error for anything. Even if an error is returned, game kit may still have enough cached information to provide an authenticated player to your game. The errors are only to assist you with debugging.

To answer your questions, first review my code example below.

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

    //Block is called each time GameKit automatically authenticates
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        [self setLastError:error];
        if (viewController)
        {
            self.authenticationViewController = viewController;
            [self disableGameCenter];
        }
        else if (localPlayer.isAuthenticated)
        {
            [self authenticatedPlayer];
        }
        else
        {
            [self disableGameCenter];
        }
    };
}

-(void)authenticatedPlayer
{
     GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [[NSNotificationCenter defaultCenter]postNotificationName:AUTHENTICATED_NOTIFICATION object:nil];
    NSLog(@"Local player:%@ authenticated into game center",localPlayer.playerID);
}

-(void)disableGameCenter
{
    //A notification so that every observer responds appropriately to disable game center features
    [[NSNotificationCenter defaultCenter]postNotificationName:UNAUTHENTICATED_NOTIFICATION object:nil];
    NSLog(@"Disabled game center");
}

In my app, the call to authenticateLocalPlayer is made only once, when the app is launched. This is because the handler is invoked automatically after that.

how do we know when this view controller gets dismissed,

You won't know when this view controller gets dismissed. The code example in the documentation says to show the view controller at the appropriate time. This means that you shouldn't necessarily show the view controller every time that game center isn't able to log in. In fact, you probably shouldn't present it immediately in the handler. You should show the view controller only when it is necessary for your player to proceed with the task at hand. It shouldn't pop up at a weird time. That is why I save the view controller, so I can display later when it makes sense to.

I need to know when to resume the game if I had to pause it because the magic GC view controller was presented.

If you setup your authentication handler to post notifications based on status changes, you can listen for the event and show a "pause menu" or something, that remains until the user chooses to resume.

how do we know if the authentication succeeded

If the authentication succeeded, then the view controller is nil, and localPlayer.isAuthenticated is true.

or not ?

If authentication failed, then localPlayer.isAuthenticated is false, and the view controller was nil. Authentication failing could have happened for a number of reasons (network etc), and you shouldn't be presenting the view controller in this case, which is why the view controller will be nil.In this scenario, you should disable game center features until the user is next logged in. Since the authentication handler is called automatically, most of the time you shouldn't need to do anything. You can always provide a means to launch the game center app from your app, if you want to prompt the user to do something in game center, which you can't do automatically through your code.

EDIT: using a flag like self.isAuthenticated (as I did above)to keep track of whether you are logged in or not is not a great idea (I didn't want to cause any confusion, so I didn't remove it). It is better to always check [GKLocalPlayer localPlayer].isAuthenticated

EDIT: Cleaned up code a bit - removed unnecessary self.isAuthenticated, and block variable which isn't required.

Solution 2

For some reason, the Game Center authentication view controller is an instance of GKHostedAuthenticateViewController which is a private class we're not allowed to use or reference. It doesn't give us any way to cleanly detect when it is dismissed (unlike instances of GKGameCenterViewController which allow us to via the GKGameCenterControllerDelegate protocol.

This solution (read workaround) works by testing in the background every quarter of a second for when the view controller has been dismissed. It's not pretty, but it works.

The code below should be part of your presentingViewController, which should conform to the GKGameCenterControllerDelegate protocol.

Swift and Objective-C provided.

// Swift
func authenticateLocalUser() {
    if GKLocalPlayer.localPlayer().authenticateHandler == nil {
        GKLocalPlayer.localPlayer().authenticateHandler = { (gameCenterViewController: UIViewController?, gameCenterError: NSError?) in
            if let gameCenterError = gameCenterError {
                log.error("Game Center Error: \(gameCenterError.localizedDescription)")
            }

            if let gameCenterViewControllerToPresent = gameCenterViewController {
                self.presentGameCenterController(gameCenterViewControllerToPresent)
            }
            else if GKLocalPlayer.localPlayer().authenticated {
                // Enable GameKit features
                log.debug("Player already authenticated")
            }
            else {
                // Disable GameKit features
                log.debug("Player not authenticated")
            }
        }
    }
    else {
        log.debug("Authentication Handler already set")
    }
}

func testForGameCenterDismissal() {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.25 * Double(NSEC_PER_SEC))), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
        if let presentedViewController = self.presentedViewController {
            log.debug("Still presenting game center login")
            self.testForGameCenterDismissal()
        }
        else {
            log.debug("Done presenting, clean up")
            self.gameCenterViewControllerCleanUp()
        }
    }
}

func presentGameCenterController(viewController: UIViewController) {
    var testForGameCenterDismissalInBackground = true

    if let gameCenterViewController = viewController as? GKGameCenterViewController {
        gameCenterViewController.gameCenterDelegate = self
        testForGameCenterDismissalInBackground = false
    }

    presentViewController(viewController, animated: true) { () -> Void in
        if testForGameCenterDismissalInBackground {
            self.testForGameCenterDismissal()
        }
    }
}

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!) {
    gameCenterViewControllerCleanUp()
}

func gameCenterViewControllerCleanUp() {
    // Do whatever needs to be done here, resume game etc
}

Note: the log.error and log.debug calls are referencing XCGLogger: https://github.com/DaveWoodCom/XCGLogger

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

    __weak __typeof__(self) weakSelf = self;
    if (!localPlayer.authenticateHandler) {
        [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError* error) {
            if (error) {
                DLog(@"Game Center Error: %@", [error localizedDescription]);
            }

            if (viewcontroller) {
                [weakSelf presentGameCenterController:viewcontroller];
            }
            else if ([[GKLocalPlayer localPlayer] isAuthenticated]) {
                // Enable GameKit features
                DLog(@"Player already authenticated");
            }
            else {
                // Disable GameKit features
                DLog(@"Player not authenticated");
            }
        })];
    }
    else {
        DLog(@"Authentication Handler already set");
    }
}

- (void)testForGameCenterDismissal
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        if (self.presentedViewController) {
            DLog(@"Still presenting game center login");
            [self testForGameCenterDismissal];
        }
        else {
            DLog(@"Done presenting, clean up");
            [self gameCenterViewControllerCleanUp];
        }
    });
}

- (void)presentGameCenterController:(UIViewController*)viewController
{
    BOOL testForGameCenterDismissalInBackground = YES;
    if ([viewController isKindOfClass:[GKGameCenterViewController class]]) {
        [(GKGameCenterViewController*)viewController setGameCenterDelegate:self];
        testForGameCenterDismissalInBackground = NO;
    }

    [self presentViewController:viewController animated:YES completion:^{
        if (testForGameCenterDismissalInBackground) {
            [self testForGameCenterDismissal];
        }
    }];
}

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController*)gameCenterViewController
{
    [self gameCenterViewControllerCleanUp];
}

- (void)gameCenterViewControllerCleanUp
{
    // Do whatever needs to be done here, resume game etc
}

Solution 3

I might be wrong, but I think there actually is a way to know when the authentication view controller gets dismissed. I believe the initial authenticate handler that you set will be called when the user dismisses the authentication view controller, except this time the viewController parameter of the handler will be nil.

The way my app works is: the authenticate handler is set at the beginning of the application, but the authentication view controller is only displayed when the user asks to view the Leaderboards. Then, when this authentication view controller is dismissed, the initial authenticate handler either displays the leaderboards if the user was authenticated or doesn't if he wasn't.

Share:
14,088

Related videos on Youtube

soleil
Author by

soleil

Updated on June 04, 2022

Comments

  • soleil
    soleil almost 2 years

    According to the Apple docs we should do something like this to handle GC authentication:

    - (void) authenticateLocalUser
    {
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
        if(localPlayer.authenticated == NO)
        {
            [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
                if (!error && viewcontroller)
                {
                    DLog(@"Need to log in");
                    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
                    [appDelegate.window.rootViewController presentViewController:viewcontroller animated:YES completion:nil];
    
                }
                else
                {
                    DLog(@"Success");
    
                }
            })];
    
        }
    }
    

    And we are given this information:

    If the device does not have an authenticated player, Game Kit passes a view controller to your authenticate handler. When presented, this view controller displays the authentication user interface. Your game should pause other activities that require user interaction (such as your game loop), present this view controller and then return. When the player finishes interacting with it, the view controller is dismissed automatically.

    My question is, how do we know when this view controller gets dismissed, and how do we know if the authentication succeeded or not?

    Obviously I need to know if the authentication worked or not, and I need to know when to resume the game if I had to pause it because the magic GC view controller was presented.

  • James Paul Mason
    James Paul Mason over 9 years
    Most of the tutorials I've found don't use this delegate at all. Do you have an example of how to set up game center with the delegate included?
  • Dave Wood
    Dave Wood over 9 years
    This delegate doesn't work for the login view controller which is an instance of GKHostedAuthenticateViewController (private), verses an instance of GKGameCenterViewController used by the GKGameCenterControllerDelegate protocol.
  • Abdul Ahmad
    Abdul Ahmad over 8 years
    Hi Dave, I have a quick question that I'm hoping you can answer without me having to provide code (otherwise I'll post a question and give you the link). My question has to do mainly with showing the game center view controller. When I do "presentGameController" my game screen presents a UIViewController, but its just a black screen. any idea why this may be happening?
  • Dave Wood
    Dave Wood over 8 years
    Sorry, that's not something I've seen happen, so I'd only be guessing. I'd post a question with some sample code.
  • Abdul Ahmad
    Abdul Ahmad over 8 years
    thanks for the quick response! I'll post a question and let you know