authPlayerWithCompletionHandler Deprecated, so how do I use authenticateHandler

ManOx picture ManOx · Sep 13, 2012 · Viewed 10.5k times · Source

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

Answer

12on picture 12on · Oct 14, 2012

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];
                }
            })];
        }
    }
}