authPlayerWithCompletionHandler Deprecated, so how do I use authenticateHandler

10,538

Solution 1

setAuthenticateHandler is new in iOS 6, authenticateWithCompletionHandler must still be used in iOS 5 and below.

Also, providing a completion handler for presentViewController:animated:completion: is not really necessary since that completion handler is called just after the game center view is displayed, not when it is completed.

Here's my solution:

NOTE - tested on iOS 4.3, iOS 5.1, iOS 6.0 simulators only - not on actual device.

NOTE - this assumes you've checked that GameCenter API is available.

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

    if (localPlayer.isAuthenticated)
    {
        /* Perform additional tasks for the authenticated player here */
    }
    else
    {
        /* Perform additional tasks for the non-authenticated player here */
    }
}

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] \
compare:v options:NSNumericSearch] == NSOrderedAscending)

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

        if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
        {
            // ios 5.x and below
            [localPlayer authenticateWithCompletionHandler:^(NSError *error)
             {
                 [self checkLocalPlayer];
             }];
        }
        else
        {
            // ios 6.0 and above
            [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
                if (!error && viewcontroller)
                {
                    [[AppDelegate sharedDelegate].viewController
                    presentViewController:viewcontroller animated:YES completion:nil];
                }
                else
                {
                    [self checkLocalPlayer];
                }
            })];
        }
    }
}

Solution 2

I am using this code for iOS 6 and above. There are no compiler errors and it seems to work fine.

#pragma
#pragma mark - Player Authentication
-(void)autheticatePlayer
{
    __weak typeof(self) weakSelf = self; // removes retain cycle error

    _localPlayer = [GKLocalPlayer localPlayer]; // localPlayer is the public GKLocalPlayer
    __weak GKLocalPlayer *weakPlayer = _localPlayer; // removes retain cycle error

    weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        if (viewController != nil)
        {
            [weakSelf showAuthenticationDialogWhenReasonable:viewController];
             }
             else if (weakPlayer.isAuthenticated)
             {
                 [weakSelf authenticatedPlayer:weakPlayer];
             }
             else
             {
                 [weakSelf disableGameCenter];
             }
             };
}

-(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
{
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
}

-(void)authenticatedPlayer:(GKLocalPlayer *)player
{
    player = _localPlayer;
}

-(void)disableGameCenter
{

}

Solution 3

This is what I came up with - it seems to work. Feel free to edit if you think I missed anything.

-(void)authenticatePlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
        if (!error) {
            [self presentViewController:viewcontroller animated:YES completion:^{
                if (localPlayer.isAuthenticated)
                {
                    // your code if authenticated
                }
                else {
                    // your code if not authenticated
                }
            }];
        }
        else {
            // error handling code here
        }
    })];
}
Share:
10,538
ManOx
Author by

ManOx

Updated on June 09, 2022

Comments

  • ManOx
    ManOx almost 2 years

    Questions pretty much in the title, authPlayerWithCompletionHandler is Deprecated, so how do I use authenticateHandler?

  • msgambel
    msgambel over 11 years
    Works well! How do you get rid of the warning flag for the deprecated method though?
  • Charlie Monroe
    Charlie Monroe almost 11 years
    Just set Deployment Target to e.g. 5.0 or whatever is your target - select the project (top row in the project navigator) > select your app in Targets > Summary > Deployment Target.