Questions pretty much in the title, authPlayerWithCompletionHandler is Deprecated, so how do I use authenticateHandler?
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];
}
})];
}
}
}
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
{
}
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
}
})];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With